0% found this document useful (0 votes)
11 views11 pages

23-55566-3 FinalLabTask02 - IDB

database
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)
11 views11 pages

23-55566-3 FinalLabTask02 - IDB

database
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/ 11

American International University-Bangladesh

(AIUB)
Introduction to Database
Final term Lab Tasks-02
Subqueries
A subquery is a SELECT statement that is embedded in a clause of another SELECT statement.
You can build powerful statements out of simple ones by using subqueries. They can be very
useful when you need to select rows from a table with a condition that depends on the data in the
table itself.
You can place the subquery in a number of SQL clauses:
 WHERE clause
 HAVING clause
 FROM clause
Guidelines for Using Subqueries
 A subquery must be enclosed in parentheses.
 A subquery must appear on the right side of the comparison operator.
 Subqueries cannot contain an ORDER BY clause. You can have only one ORDER BY
clause for a SELECT statement, and if specified it must be the last clause in the main
SELECT statement.
 Two classes of comparison operators are used in subqueries: single-row operators and
multiple-row operators.
Errors with Subqueries
One common error with subqueries is more than one row returned for a single-row subquery.
To correct this error, change the = operator to IN.
Exercise
1) Display all the employees who are earning more than all the managers.
Ans.:
select * from emp
where sal > all
(select sal from emp
where job = 'MANAGER')
and job <> 'MANAGER'

2) Display all the employees who are earning more than any of the managers.
Ans.:
select * from emp
where sal > any
(select sal from emp
where job = 'MANAGER')
and job <> 'MANAGER'
3) Select employee number, job & salaries of all the Analysts who are earning more than any of
the managers.
Ans.:
select empno,job,sal from emp
where sal > any (select sal from emp
where job ='MANAGER')
and job='ANALYST'
4) Select all the employees who work in DALLAS.
Ans.:
select * from emp
where deptno in (select deptno from dept where loc = 'DALLAS')

5) Select department name & location of all the employees working for CLARK.
Ans.:
select dname,loc from dept
where deptno in(select deptno from emp where job ='CLERK')

6) Select all the departmental information for all the managers


Ans.:
select * from dept
where deptno in (select deptno from emp where job ='MANAGER')
7) Display the first maximum salary.
Ans.:
select sal from emp
where sal = (select max(sal) from emp)

8) Display the second maximum salary.


Ans.: select max(sal) from emp
9) Display the third maximum salary.
Ans.:
select max(sal) from emp
where sal <
(select max(sal) from emp
where sal <(select max(sal) from emp))
10) Display all the managers & clerks who work in Accounts and Marketing departments.
Ans.:

select * from emp


where deptno in (select deptno from dept where dname='ACCOUNTING' or dname
='MARKETING')
and job in('CLERK','MANAGER')

11) Display all the salesmen who are not located at DALLAS.
Ans.: select * from emp
where deptno in (select deptno from dept where loc <>'DALLAS')
and job ='SALESMAN'
12) Get all the employees who work in the same departments as of SCOTT.
Ans.:
select * from emp
where deptno = (select deptno from emp where ename='SCOTT')

13) Select all the employees who are earning same as SMITH.
Ans.:
select * from emp where sal =(select sal from emp where ename='SMITH')
14) Display all the employees who are getting some commission in marketing department where
the employees have joined only on weekdays.
Ans.:

15) Display all the employees who are getting more than the average salaries of all the
employees.
Ans.:
SELECT * FROM EMP WHERE SAL >(SELECT AVG(SAL) FROM EMP)

You might also like