Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 24

SUB QUERY

SUBQUERY
It means placing a query inside the predicate of
another query, and using the inner query’s output in
the predicate’s true and false condition
Or
The value for ‘WHERE’ clause can be obtained
from another ‘SELECT’ clause.
SUBQUERY
Example:
Get the detail of the employee who earns maximum
salary.
Approach: first of all develop the inner query which
calculate the max salary
In the second step write a outer query which show
the details of employee by equating the sal of emp
with the maximum salary outputted by the above
inner query.
SIMPLE SUBQUERY
• SELECT ENAME, EMPNO, DEPTNO,
SAL FROM EMP WHERE SAL =
(SELECT MAX(SAL) FROM EMP);

Inner subquery

Outer subquery
SUBQUERY
• Subqueries are used in the WHERE or
HAVING clause of another SQL statement.
• The subquery must be enclosed in the
paranthesis
• You can reference columns from the outer
query in the inner query.
• Outer column can appear both in SELECT
and WHERE clause
SUBQUERY- NESTING
• The subqueries can be further nested
• There can be 255 subquries in the nest.
• The innermost query is executed first
Nested subquery
• SELECT * FROM EMP WHERE SAL >
(SELECT MAX(SAL) FROM EMP
WHERE DEPTNO = (SELECT DEPT NO
FROM DEPT WHERE RTRIM(DNAME)
= ‘SALES’));
SUBQUERY

Subqueries can be divided into two broad


groups.
• Single row subquery – this is a subquery
which returns only one value to the outer
query as in the last example
• Multi row subquery – this is a subquery
which returns multiple values to the outer
query.
OPERATORS
In the last example we have seen that the inner query
has returned only one value. Had it returned more
than one value then this would have resulted in an
error. So to handle multiple values several
operators are used.
• OPERATORS
– IN OR NOT IN
– ANY OR ALL
– EXISTS
– HAVING
IN
SELECT ENAME, EMPNO,DEPTNO,SAL
FROM EMP WHERE (DEPTNO,SAL) IN
(SELECT DEPTNO, MAX(SAL) FROM
EMP GROUP BY DEPTNO)
ANY
• If the relation required for the outer column
in the WHERE clause is such that it should
be TRUE for any value in the list of values
returned by the inner query then the ANY
operator is used.
ANY

Find the details of the employees in deptno 20


whose salary is greater than any other
employees in the emp table

• SELECT ENAME ,EMPNO, JOB, SAL


FROM EMP WHERE SAL >
ANY(SELECT DISTINCT SAL FROM
EMP) AND DEPTNO =20;
ALL
• If the relation required for the outer column
in the WHERE clause is such that it should
be TRUE for all the values in the list of
values returned by the inner query ; then the
ALL operator is used.
ALL
• SELECT * FROM EMP WHERE SAL >=
ALL (SELECT SAL FROM EMP WHERE
SAL != (SELECT MAX(SAL) FROM
EMP))
The IN operator is equivalent to ‘=ANY’
The ‘NOT IN ‘ operator is equivalent to ‘!
=ALL)
EXISTS
It takes a subquery as an argument and
evaluates to true if it produces any output or
false if it does not.
This is used for example when we decide to
extract some data from emp table when it
contains the deptno as 10. The subquery is
performed only once for the entire subquery
and therefore had it a single value for all
cases.
Example

SELECT
E.ENAME,E.EMPNO,E.JOB,E.DEPTNO
FROM EMP E WHERE exists (select * from
Emp where deptno=10)

In the above example inner subquery will be


Evaluated first. If some output is there then
EXISTS will evaluate it to true and it will
execute the outer query
CORRELATED SUBQUERY
• WHEN ONE QUERY DEPENDS ON
THE OTHER.
• When you are using subqueries in SQL,
you can refer in the inner query to the
table in the FROM clause of the outer
query, forming a correlated subquery.
When you do this, the subquery is
executed repeatedly, once for each row of
the main query’s table
CORRELATED SUBQUERY
• SELECT E.ENAME,E.EMPNO,
E.SAL,E.DEPTNO FROM EMP E
WHERE E.SAL > (SELECT
AVG(F.SAL) FROM EMP F WHERE
F.DEPTNO = E.DEPTNO);
CORRELATED SUBQUERY
Steps for evaluating the correlated subquery
•Select a row from the table named in the outer query.
This will become the current candidate now.
•Perform the subquery. Use the value from the current
candidate row. The use of a value from the outer
query’s candidate row in a subquery is called an outer
reference.
•Evaluate the predicate of the outer query on the basis
of the results of the subquery performed in the above
step. This will determine whether the candidate row is
selected for the output.
•Repeat the procedure for the next candidate row of
the table and so on until all the rows of the table have
been tested.
Using EXISTS with the correlated query

• With a correlated subquery, the


EXISTS clause is evaluated separately
for each row of the table referenced in
the outer query.
• So it can generates different answers
for each row of the table referenced in
the main query.
Using EXISTS with the correlated query

• SELECT
E.ENAME,E.EMPNO,E.JOB,E.DEPTNO
FROM EMP E WHERE EXISTS (SELECT
F.DEPTNO FROM EMP F WHERE
F.MGR =E.EMPNO);
• So what is output of the above query.
EXISTS

SELECT
E.ENAME,E.EMPNO,E.JOB,E.DEPTNO
FROM EMP E
WHERE e.empno in (SELECT distinct mgr
from emp)

The last EXISTS subquery can be written in


a more simpler way as shown above
NOT EXISTS
• SELECT * FROM DEPT D WHERE NOT
EXISTS (SELECT * FROM EMP F
WHERE F.DEPTNO=D.DEPTNO)

• What is output of the above query


• This is the inverse of the EXISTS

You might also like