Professional Documents
Culture Documents
Cust12 Cid Cid Orders Amt
Cust12 Cid Cid Orders Amt
SQL> UPDATE orders SET amount = amount + 1000 WHERE cid IN (SELECT cid FROM
order12 where amt>2000);
1 row updated.
1 Rohan SQL
2 Rajiv PL SQL
3 Ram Java
Salary Table :
Employee No Salary
1 25000
2 35000
3 45000
to delete data from Employee table where salary is greater than 25000.
Query :
Delete from empl where eno IN
( Select eno From Salary where Salary > 25000 );
DELETE FROM orders WHERE oid IN (SELECT oid FROM order12 WHERE cid = 5);
1 row deleted.
SQL> select cid, name from cust12 union select id, name from cust21;
CID NAME
---------- ----------
1 Apple
2 Khilan
2 bb
3 kaushik
4 Chaitali
4 dd
5 Hardik
5 ee
6 Komal
6 ff
7 bb
CID NAME
---------- ----------
11 arun
12 rows selected.
SQL> select cid, name from cust12 union select id, name from cust21 order by name
desc;
CID NAME
---------- ----------
3 kaushik
6 ff
5 ee
4 dd
2 bb
7 bb
11 arun
6 Komal
2 Khilan
5 Hardik
4 Chaitali
CID NAME
---------- ----------
1 Apple
12 rows selected.
SQL> select cid, name from cust12 intersect select id, name from cust21;
CID NAME
---------- ----------
1 Apple
ID
----------
3
11
CID
----------
7
SQL> select cid,id, cust21.name, cust12.name from cust12 inner join cust21 on
cust12.cid=cust21.id;
10 rows selected.
SQL> select cid,c1.name, id, c2.name from cust12 c1 left join cust21 c2 on
c1.cid=c2.id;
11 rows selected.
SQL> select cid,c1.name, id, c2.name from cust12 c1 right join cust21 c2 on
c1.cid=c2.id;
14 rows selected.
SQL> select cid,c1.name, id, c2.name from cust12 c1 full join cust21 c2 on
c1.cid=c2.id;
15 rows selected.
SQL>
SQL> select * from cust12 where cid in (1,2,4,5);
SQL> select name from empl where sal in(select max(sal) from empl);
NAME
------------
aaa
finds all employees who salaries are greater than the average salary of all
SQL> select name from empl where sal >(select avg(sal) from empl);
NAME
------------
aaa
SQL> select name from empl where sal <(select avg(sal) from empl);
NAME
------------
bbb
eee
fff
SQL>
SQL> select name,sal from empl where sal <(select avg(sal) from empl);
NAME SAL
------------ ----------
bbb 30000
eee 80000
fff 90000
finds all departments which have at least one employee with the salary is greater
than 10,000:
SELECT
deptname
FROM
dept d
WHERE
EXISTS( SELECT
1
FROM
empl e
WHERE
sal > 10000
AND e.deptid = d.deptid)
ORDER BY deptname;
DEPTNAME
----------
SCSE
SECE
SQL>
SELECT
deptname
FROM
dept d
WHERE
EXISTS( SELECT
1
FROM
empl e
WHERE
sal < 10000
AND e.deptid = d.deptid)
ORDER BY deptname;
DEPTNAME
----------
Select
The following example finds all employees whose salaries are greater than the
lowest salary of every department:
SELECT
eid, name, sal
FROM
empl
WHERE
sal >= ALL (SELECT
MIN(sal)
FROM
empl
GROUP BY deptid)
ORDER BY name;
EID NAME SAL
---------- ------------ ----------
11 aaa 200000
55 eee 80000
66 fff 90000
The SOME operator in SQL must match at least one value in a subquery and that
value must be preceded by comparison operators.
finds all employees whose salaries are greater than or equal to the highest salary
of every department.
SELECT
eid, name, sal
FROM
empl
WHERE
sal >= some(SELECT
Max(sal)
FROM
empl
GROUP BY deptid)
To find all employees whose salaries are equal to the average salary of their
department, you use the following query:
SELECT
first_name,
last_name,
salary
FROM
employees
WHERE
salary = ANY (
SELECT
AVG(salary)
FROM
employees
GROUP BY
department_id)
The SQL ANY and ALL Operators. The ANY and ALL operators are used with a
WHERE or HAVING clause. The ANY operator returns true if any of the
subquery values meet the condition. The ALL operator returns true if all of the
subquery values meet the condition.
suppose a request is received to list the depts which do not have an assigned employees yet
SELECT deptID
FROM empl
);
DEPTID
----------
3
SQL> SELECT dept.deptID FROM dept
2 WHERE EXISTS (
3 SELECT deptID
4 FROM empl
5 WHERE empl.deptID = dept.deptID
6 );
DEPTID
----------
1
2
6 rows selected.
ID NAME CITY
---------- ---------- ----------
1 Apple chennai
2 bb tirunel