Unit-2 SQL Updated
Unit-2 SQL Updated
LANGUAGE (SQL)
Example:
create table branch
(branch_namechar(15) not null,
branch_city char(30),
assets integer);
MBA-17 Batch (2020-22)
INTEGRITY CONSTRAINTS IN
CREATE TABLE
not null
primary key (A1, ..., An )
Drop table r;
Examples:
Insert into CUSTOMERS values
Examples:
Delete from ITEMS where Item_no=25;
Delete from ITEMS;
Delete from STORE where Item_no IN (select Item_no
from ITEMS where weight=4);
SET (target_value_list)
WHERE (predicate);
- Update STORE
set QTY_HELD= 700
where ITEM_NO in (select ITEM_NO from
ITEMS where WEIGHT=>6);
OUTPUT:-
+------+---------+------------+-----+-------+-
| s_id | s_name | s_phn | CNT |
+------+---------+------------+-----+--------+-
| 1 | Anchal | 6363444659 | 2 |
+------+---------+------------+-----+---------+
CONT…..
MBA-17 Batch (2020-22)
Inserting one more same name(Dazzy) with
different phone number in previous table &
again counting the duplicate records.
mysql> insert into
customer(s_id,s_name,s_phn)values(003,‘Dazzy'
,1234588);
mysql> select * from customer;
+------+---------+------------+------+
| s_id | s_name | s_phn |
+------+---------+------------+-------+
| 1 | Anchal | 6363444659 |
| 1 | Anchal | 6363444659 |
| 2 | Dazzy | 1234577 |
| 2 | Dazzy | 1234588 |
MBA-17 Batch (2020-22)
CONT…
Again applying the query of duplicate records
–
select s_id, s_name, s_phn, COUNT(1) as CNT
from customer group by s_id, s_name, s_phn
having COUNT(1)>1;
OUTPUT:-
+------+---------+------------+-----+-----+
| s_id | s_name | s_phn | CNT |
+------+---------+------------+-----+------+
| 1 | Anchal | 6363444659 | 2 |
| 2 | Dazzy | 1234577 | 2 |-
change in phone number(Dazzy-1234588) than
also it is counting the name as same & giving
the count 2.
CONT…
SOLUTION:-
mysql> select s_id, s_name,count(s_phn)
from customer group by s_id;
+------+---------+--------------+------+
| s_id | s_name | count(s_phn) |
+------+---------+--------------+------+
| 1 | Anchal | 2 |
| 2 | Dazzy | 1 |
| 3 | Dazzy | 1 |
+------+---------+--------------+-----+
+-----------------+-----------+-----------+---------------+----
|CAND_NAME | stud_ID| room_no | mob_no |
+-----------------+-----------+------------+--------------+-
| ANCHAL | 801 | 11 |7696555803|
| SCOOBY | 802 | 12 | NULL |
MBA-17 Batch (2020-22)
SUB STRING
A subquery can have only one column in the SELECT clause, unless
multiple columns are in the main query for the subquery to compare its
selected columns.
Subqueries that return more than one row can only be used with
multiple value operators such as the IN operator.
SELECT *
FROM CUSTOMERS
WHERE ID IN
(SELECT ID
FROM CUSTOMERS
WHERE SALARY > 4500) ;
UPDATE CUSTOMERS
SET SALARY = SALARY * 0.25
WHERE AGE IN
(SELECT AGE
FROM CUSTOMERS_EMP
WHERE AGE >= 27 );
1 abhi 1 DELHI
ID NAME ID Address
2 adam 1 DELHI
1 abhi 2 MUMBAI
1 abhi 3 CHENNAI
2 adam 3 CHENNAI
4 alex 3 CHENNAI
SELECT column-name-list
FROM table-name1 LEFT OUTER JOIN table-name2
ON table-name1.column-name = table-name2.column-name;
3 alex 3 CHENNAI
3 alex 3 CHENNAI
null null 7 NOIDA
4 anu 7 NOIDA
null null 8 PANIPAT
5 ashish 8 PANIPAT
MBA-17 Batch (2020-22)
RIGHT OUTER JOIN
Right outer Join Syntax for Oracle is,
SELECT column-name-list
FROM table-name1, table-name2
ON table-name1.column-name(+) = table-name2.column-name;