Sub Queries with Delete Command
Delete all Employee Records whose job=job of smith
delete from emp where job=(select job from emp where ename='smith')
Correlated Sub Queries
When a sub query refers to a column of Main Query within the condition in “where clause” than it is called as CO RELATED sub queries
In co related sub queries execution starts from outer query for every record of outer query table inner query is executed once.
Display employee records earning more than avg salary of their Dept.
select * from emp a where a.sal>(select avg(Sal) from emp where DEPTNO=a.DEPTNO)
Find the employees whose salary is more than average salary of same department in which that department is working
select * from emp a where sal>(select avg(sal) from emp s where s.DEPTNO=a.deptno)
In co-Related Sub Queries we use Two options
Exists
Non Exists
These are the predicates that are generally used with co-related sub query and they are used to determine whether or not the sub query returns at least one-Row
The predicate Exists will return true, when the sub query Return any Rows and otherwise it Returns False
NonExists will return true, when the sub query does not return any Rows and otherwise it returns False
Find the employees to whom atleast one of employee is reporting
select * from emp m where exists(select * from emp e where e.mgr=m.EMPNO)
Display the details of departments in which they are no employees
select * from DEPT d where not exists(select * from emp e where e.DEPTNO=d.DEPTNO)
Display the details of departments in which are not empty
select * from DEPT d where exists(select * from emp e where e.DEPTNO=d.DEPTNO)
Find the 3 highest salaried employee
select * from emp e where 2=(select count(distinct sal) from emp s where s.sal>e.sal)
Fnd the n th highest salary
select * from emp e where n-1=(select count(distinct sal) from emp s where s.sal>e.sal)
Find the employees drawing top 3 highest salaries
select * from emp e where 2>=(select count(distinct sal) from emp s where s.sal>e.sal)
Find the employee drawing 3rd lowest salary
select * from emp e where 2=(select count(distinct sal) from emp s where s.sal<e.sal)
Find the employee drawing top 3 lowest salaries
select * from emp e where 2>=(select count(distinct sal) from emp s where s.sal<e.sal)
Display the list of manager
select * from emp e where exists(select * from emp where MGR=e.EMPNO)
Note: sql server recommends use Exists, Not Exists instead of IN AND NOT IN OPERATOR
Difference between Join and Sub query
Use sub queries we get data from one table and condition based on another table
Use join operation to get data from multiple tables.
Derived Tables
This is introduced from sql server 2005
Sub queries follows from clause are called as Derived Tables
Syntax:
Select * from (select statement) as alias
Use Derived Tables to simplify the complex Processing
To use result of one query in another query
select * from (select empno,ename ,sal*12 annasal from emp ) as e where annasal>60000
Display top maximum 3 salaries in emp table
select distinct sal from (select ename, sal , DENSE_RANK() over (order by sal desc) rank from emp) as e where rank<=3
Scalar Queries
Sub queries follows select clause are called as “Scalar Queries”
Syntax:
Select (select statement)
(select statement) from <tablename>
select
(select max(sal) from emp where DEPTNO=10) D10,
(select MAX(sal) from emp where DEPTNO=20)D20,
(select max(sal) from emp where deptno=30) D30
Sub query examples
Write a query to find the details of employee earning the highest salary
select *from EMP whereSAL=(select MAX(sal) from EMP)
Write a quey to find the details of employee earning the second highest salary
select * from EMP where SAL=(select MAX(sal) from EMP
where sal<(select MAX(Sal) from EMP))
Write a query to find the details of employees working in sales dept
select * from EMP where DEPTNO=(select DEPTNO fromDEPT where DNAME='sales')
Write a query to find the details of employee working in Mumbai
select * from EMP where DEPTNO=(select DEPTNO fromDEPT where loc='mumbai')
Write query to find the details of employee who are earning more than the highest salary of deptno 30
select *from EMP whereSAL>(select MAX(Sal) from EMP where DEPTNO=30)
Write a query to find the details of employee who are earning less than the lowest salary of deptno 20
select *from EMP whereSAL< (select min(Sal) from EMP where DEPTNO=20)