0% found this document useful (0 votes)
313 views

Sub Query

This document contains several SQL queries with answers related to subqueries. The queries cover topics like finding employees with salaries above or below average, employees in a certain department, employees managed by a certain manager, and more. Multiple types of subqueries are used like single-row, multiple-row, correlated, and analytic functions.

Uploaded by

Avulaiah G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
313 views

Sub Query

This document contains several SQL queries with answers related to subqueries. The queries cover topics like finding employees with salaries above or below average, employees in a certain department, employees managed by a certain manager, and more. Multiple types of subqueries are used like single-row, multiple-row, correlated, and analytic functions.

Uploaded by

Avulaiah G
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Sub

query(queries)
Q) Write a query to display the employees who are getting more salary than
the avg(sal) from emp?
Ans: SQL> select ename, sal from emp where sal>(select avg(sal) from emp);
Q) Write a query to display the employees who are working in sales
department from emp, dept tables.?
Ans: SQL> select ename from emp where deptno=(select deptno from dept
where dname=‟SALES‟);
Q) Write a query to display the employees who are working in “SMITH”
department number from emp table?
Ans: SQL> select ename from emp where deptno=(select deptno from emp
where ename=‟SMITH‟);
Q) Write a query to display senior most employee details from emp table?
Ans: SQL> select * from emp where hiredate=(select min(hiredate) from emp);
Q) Write a query to display highest sal details emp table?
Ans: SQL> select * from emp where sal=(select max (sal) from emp);
Q) Write a query to display highest paid emp department, dname from emp,
dept tables?
Ans: SQL> select dname from dept where deptno=(select deptno from emp
where sal=(select sal(max) from emp));
Q) Write a query to display second max(sal) from emp table?
Ans: SQL> select max(sal) from emp where sal select * from emp where
sal=(select max(sal) from emp where sal.
Q) Write a query to display the employees who are working under „BLAKE‟
from emp table using „empno, mgr‟ columns ?
Ans: SQL> select * from emp where mgr= (select empno from emp where
ename=‟BLAKE‟);
Q) Write a query to display lowest average salary job from emp tables?
Ans: SQL> select job, avg(sal) from emp group by job having avg(sal)=(select
min(avg(sal)) from emp);
Q) Write a query to display lowest average salary job from emp table where
deptno less than 30?
Ans: SQL> select deptno, min(sal) from emp group by deptno having
min(sal)>(select min(sal) from emp where deptno= 30);
Q) Write a query to display job, avg(sal) of the employees whose job avg(sal)
more than the clerk‟s job avg(sal) from emp table?
Ans: SQL> select avg(sal), job from emp group by job having avg(sal) >(select
avg(sal) from emp where job=‟CLERK‟)
Q) Write a query to display the employees who are working sales department
from emp, dept tables?
Ans: SQL> select ename, dname from emp e, dept d where e.deptno=d.deptno
and d.deptno=(select deptno from dept where dname=‟SALES‟)
Q) Write a query to display the employees who are getting more salary than
the „BLAKE‟ salary from emp table?
Ans: SQL> select * from emp where sal> (select sal from emp where
ename=‟BLAKE‟)
MULTIPLE ROW SUB QUERY‟S:
Q) Write a query to display the employee details who are getting max(sal) in
each department from emp table?
Ans: SQL> select * from emp where sal= (select max(sal) from emp group by
deptno);
Q) Write a query to display the employees who are working in sales or
research department from emp, dept?
Ans: SQL> select ename, deptno from emp where deptno in(select deptno
from dept where dname=‟SALES‟ or dname=‟RESEARCH‟);
Q) Write a query to display the employees who are working as “supervisors”
(managers) from emp table using empno, mgr?
Ans: SQL> select * from emp where empno in(select mgr from emp);
Q) Write a query to display the employees who are not working as
“supervisors” (managers) from emp using empno, mgr?
Ans: SQL> select * from emp where empno not in(select nvl(mgr,0) from emp);
TOP N Analysis:
1. Inline view
2. Rownum
Q) Write a query to display first row from emp table using rownum?
Ans: SQL> select * from emp where rownum=1;
Q) Write a query to display second row from emp table using rownum?
Ans: SQL> select * from emp where rownum=2; No rows selected Generally,
rownum doesn‟t work with more than “1” +ve integer, i.e., it works with <=
operators.
Q) Write a query to display first five rows from emp table using rownum?
Ans: SQL> select * from emp where rownum<=5;
Q) Write a query to display first five highest salary employees from emp using
rownum?
Ans: SQL> select * from( select * from emp order by sal desc) where
rownum<=5;
Q) Write a query to display 5th highest salary employee from emp table using
rownum?
Ans: SQL> select * from(select * from emp order by sal desc) where
rownum=5 minus select * from(select * from emp order by sal desc) where
rownum<=4;
Q) Write a query to display rows between 1 to 5 from emp table using
“Rownum”?
Ans: SQL> select * from emp where rownum between 1 and 5;
Q) Write a query to display rows between to 5 to 9 from table?
Ans: SQL> select * from emp where rownum between 5 and 9; No rows
selected Solution: SQL> select * from emp where rownum <=9 minus select *
from emp where rownum<=5;
Q) Write a query to display second record from emp using rownum?
Ans: SQL> select * from emp where rownum<=2 minus select * from emp
where rownum<=1;
Q) Write a query to display last two records from emp table using rownum?
Ans: SQL> select * from emp where rownum<=14
Minus
select * from emp where rownum<=12;
Solution: SQL> select * from emp minus select * from emp where
rownum<=(select count(*)-2 from emp);
SQL> select * from emp where rownum>1;
No rows selected
SQL> select * from emp where rownum>=1;
Q) Write a query to display second record from emp table using “rownum”
alias name?
Ans: SQL> select * from(select rownum r, ename, job, sal from emp) where
r=2;
Q) Write a query to display second, third, fifth, seventh, eighth rows from emp
table using rownum alias name?
Ans: SQL> select * from (select rownum r, e.* from emp e) where r
in(2,3,5,7,8);
Q) Write a query to display records from 5 to 9 from emp table using rownum
alias name?
Ans: SQL> select * from(select rownum r, emp.* from emp) where r between
5 and 9;
Q) Write a query to display first and last records from emp table using rownum
alias name?
Ans: SQL> select * from(select rownum r, emp.* from emp) where r=1 or
r=(select count(*) from emp);
Q) Write a query to display even number of records from emp table using
rownum alias name?
Ans: SQL> select * from(select rownum r, emp.* from emp) where r in(select
mod(r,2)=0 from emp);
Solution: SQL> select * from(select rownum r, emp.* from emp) where
mod(r,2)=0;
Q) Write a query to display skip first 5 rows from emp table using rownum
alias name?
Ans: SQL> select * from (select rownum r, emp.* from emp) where r>5;
ROWID
Eg: SQL> select rownum, rowid, ename from emp where deptno=10; In Oracle,
by default rowid‟s are ascending order.
Q) Write a query to display second record from emp table using analytical
function, rowid?
Ans: select * from(select deptno, ename, sal, row_number() over(order by
rowid)r from emp) where r=2;
Q) Write a query to display each department second row from emp table using
analytical function, rowid?
Ans: SQL> select * from(select deptno, ename, sal, row_number() over
(partition by deptno order by rowid) r from emp where r=2);
Q) Write a query to display last two records from emp table using analytical
function, rowid?
We can also use max(), min() functions in “rowid‟s”.
Eg: SQL> select max(rowid) from emp; SQL> select max(rowid) from emp;
Q) Write a query to display first row from emp table using rowid?
Ans: SQL> select * from emp where rowid=(select min(rowid) from emp);
Q) Write a query to display last record from emp table using rowid?
Ans: SQL> select * from emp where rowid=(select max(rowid) from emp);
Ans: SQL> select * from(select deptno, ename, sal, row_number() over (order
by rowid desc) r from emp)where r<=2;
Q ) Display the emp records who are earning more than BLAKE:
SELECT * FROM emp
WHERE sal>(SELECT sal FROM emp
WHERE ename='BLAKE');
Q)Display the emp records who
joined after 'FORD' [OR]
Q)Display the juniors of 'FORD':
SELECT empno,ename,hiredate
FROM emp
WHERE hiredate > '23-NOV-1981';
Display the emp records who joined before FORD [OR]
Display the emp records who are senior to FORD:
SELECT empno,ename,hiredate FROM emp
WHERE hiredate<(SELECT hiredate FROM emp
WHERE ename='FORD');

Display the emp records who joined on FORD's joining date:


SELECT empno,ename,hiredate
FROM emp
WHERE hiredate = (SELECT hiredate FROM emp
WHERE ename='FORD');

Update 7369 empno salary with deptno 30's max salary:


UPDATE emp SET sal=(SELECT max(Sal) FROM emp
WHERE deptno=30)
WHERE empno=7369;
Find most senior's hiredate:
SELECT min(hiredate) FROM emp;
Find the emp name who is most senior in all
employees:
SELECT * FROM emp
WHERE hiredate = (SELECT min(hiredate)
FROM emp);
Find most junior's hiredate:
SELECT max(hiredate) FROM emp;
Find the emp name who is most junior in all
emps:
SELECT * FROM emp
WHERE hiredate = (SELECT max(hiredate)
FROM emp);
Find max Sal in all employees:
SELECT max(Sal) FROM emp;
Find the emp name who is earning maximum
salary:
SELECT * FROM emp
WHERE sal = (SELECT max(Sal) FROM emp);
Find the min salary in all employees:
SELECT min(Sal) FROM emp;
Find the emp name who is earning min salary
in all emps:
SELECT ename FROM emp
WHERE sal=(SELECT min(sal) FROM emp);
Find 2nd max salary:
• find salary values < max sal
• From that list find max value
SELECT max(sal) FROM emp
WHERE sal < (SELECT max(sal) FROM emp);
Find 3rd maximum salary:
• Find sal <second max salary
• From this list find max value
SELECT max(sal) FROM emp
WHERE sal < (SELECT max(sal) FROM emp
WHERE sal< (SELECT max(sal) FROM emp));
Find the emp name who is earning second max salary:
SELECT ename FROM emp
WHERE sal=(SELECT max(sal) FROM emp
WHERE sal<(SELECT max(sal) FROM emp));

Find the emp name who is earning 3rd max salary:


SELECT ename FROM emp
WHERE sal=(SELECT max(Sal) FROM emp
WHERE sal<(SELECT max(Sal) FROM emp
WHERE sal<(SELECT max(Sal) FROM emp)));

Find the emp name who is earning min salary:


Find 2nd min salary:
Find 3rd min salary:
Find the ename who earning 2nd min salary:
Find the ename who is earning 3rd min salary: Find the maximum sum of
amount in all depts:

Dept wise sum of salaries:


SELECT deptno,sum(sal) FROM
emp GROUP BY deptno; Find the maximum sum of amount in all
depts:
SELECT max(sum(Sal)) FROM emp
GROUP BY deptno;
Find the deptno which is spending max
amount on their employees:
SELECT deptno FROM emp
Oracle 6 PM Page 143

SELECT deptno FROM emp


GROUP BY deptno
HAVING sum(sal) = (SELECT max(sum(sal))
FROM emp
GROUP BY deptno);
Find the dept name which is spending max
amount on their employees:
SELECT dname FROM dept
WHERE deptno=20;
SELECT dname FROM dept
WHERE deptno=(SELECT deptno
FROM emp
GROUP BY deptno
HAVING sum(Sal) = (SELECT max(sum(sal))
FROM emp
GROUP BY deptno) );

Find dept wise number of employees:


SELECT deptno,count(*)
FROM emp
GROUP BY deptno;
Find the deptno which is having maximum
number of emps:
SELECT deptno
FROM emp
GROUP BY deptno
HAVING count(*) = (SELECT max(count(*))
FROM emp
GROUP BY deptno);
Find the dept name which is having max no of
emps:
SELECT dname FROM dept
WHERE deptno=( SELECT deptno
FROM emp
GROUP BY deptno
HAVING count(*) = (SELECT max(count(*))
FROM emp
GROUP BY deptno));
Display all managers & clerks records:
SELECT empno,ename,job,sal,deptno
FROM emp
WHERE job IN('MANAGER','CLERK');
Display all emps records whose job title is
equals to BLAKE & JAMES:
SELECT * FROM emp
WHERE job IN(SELECT job FROM emp
WHERE ename IN('BLAKE','JAMES') );

Oracle 6 PM Page 144

Display the emp records who are earning


1250 & 3000:
SELECT * FROM emp
WHERE sal IN(1250,3000);
Display the emp records whose salary is
equals to WARD's salary & FORD's salary:
SELECT empno,ename,sal
FROM emp WHERE sal IN(SELECT sal
FROM emp
WHERE ename IN('WARD','FORD') ); Display all emp records who are earning
more
than 1500 or more than 2850:
SELECT empno,ename,sal
FROM emp
WHERE sal>ANY(1500,2850);
Display the emp records whose sal
is greater than 1500 and also
greater than 2850:
SELECT empno,ename,sal
FROM emp
WHERE sal>ALL(1500,2850);
Display the emp records whose sal
is greater than 1500 and also
greater than 2850:
SELECT empno,ename,sal
FROM emp
WHERE sal>ALL(1500,2850);

You might also like