SQL part III.docx
SQL part III.docx
Sub queries
Nested sub queries
A sub query is a select statement that is nested within another select statement and which
returns intermediate results.
Select column 1, column 2, …….. from table where column = (Select column from table
where condition);
The sub query is often referred to as inner select or sub select; it generally executes first and
its output is used to complete the query condition for the main or outer query. The nested sub
query is important when we feel to select rows from a table with a condition that depends on
the data in the table itself.
Single Row sub queries
Find the employee who earns the minimum salary in the company:
Step 1: find the minimum salary: select min(sal) from emp;
Step 2: find the employee who earns the minimum salary: select ename, job, sal from emp
where sal = (result of step 1);
[The inner select is processed first producing a result. The main query block is processed then
and uses the value returned by the inner query to complete its search condition.]
Find all employees who have the same job as BLAKE:
Select ename, job from emp where job = (Select job from emp where ename = ‘BLAKE’);
Select ename, job, hiredate, sal from emp where sal > (Select max(sal) from emp where
deptno = (select deptno from dept where dname=’SALES’));
N.B The limit on the level of nesting is upto 255.
Properties of Sub Query:
1) The inner query must be enclosed in parenthesis and must be on the right hand side of
the condition.
2) The sub query may not have an order by clause.
3) The order by clause appears at the end of the main select statement.
4) Multiple columns on the select list of the inner query must be in the same order as the
columns appearing on the condition clause of the main query.
5) Sub queries are always executed from the most deeply nested to the least deeply
nested, unless they are correlated sub queries.
6) Logical and SQL operators may be used as well as ANY and ALL.
7) Returns one or more rows.
8) Returns one or more columns.
9) Use group by or group functions.
10) Join tables.
11) Retrieve from a different table other than the outer query.
12) Correlate with an outer query.
13) Use set operators.
[This is a correlated sub query because a column from the outer select is used in the where
clause of the inner select].
Operators in Correlated sub queries:
The Exist operator is frequently used with correlated sub query. It tests whether a value is
there or not. If the value exists it returns TRUE and FALSE otherwise.
Example:
Find employees who have at least one person reporting to them:
Select empno, enname, job, deptno from emp e where exists(select empno from emp where
emp.mgrno = e.empno) order by empno;
Find all employees whose department is not in the Dept table:
Select empno, ename, deptno from emp where not exists (select deptno from dept where
dept.deptno = emp.deptno);
Find the department which doesnot have any employee:
Select deptno, dname from dept d where not exists (select 1 from emp e from e.deptno =
d.deptno);