8/9/2019 Week 2a Date
1/29
8/9/2019 Week 2a Date
2/29
ObjectivesAfter completing this lesson, you should be able to do the following:
Describe the types of problem that subqueries can solve
Define subqueries
List the types of subqueries
Write single-row and multiple-row subqueries
8/9/2019 Week 2a Date
3/29
Using a Subquery to Solve a Problem
Which employees have salaries
greater than Turner salary?
What is Turner salary?
Who has a salary greater than Turners ?
Suppose you want to write a query to find out who earns a salary greater than
Turners salary.
To solve this problem, you need 2 queries.
One to find what Turner earns, andSecond query to find who earns more than that amount.
You can solve this problem by combining the 2 queries,placing one query
INSIDE the other query.
The inner query, also called the SUBQUERY, returns a value that is used byMAIN QUERY.
8/9/2019 Week 2a Date
4/29
SELECT ENAME,SAL FROM EMP
WHERE
SAL> (SELECT SAL FROM EMP WHERE ENAME='TURNER')
8/9/2019 Week 2a Date
5/29
SUBQUERY SYNTAX
SELECT select_list
FROM table
WHERE operator(SELECT select_list FROM table ) ;
Subquery includes
WHERE clause
HAVING clause
FROM clause
Operator includes
Single row : = < >=
Multiple row : IN ANY ALL
8/9/2019 Week 2a Date
6/29
TYPES OF SUBQUERY
Main Query
Subquery FORD
Main Query
SubqueryFORD
TURNER
SCOTT
Single-Row subquery
Multiple-Row subquery
returns
returns
Return only
one row from
the inner
select
statement.
Return more
than one row
from the
inner select
statement.
8/9/2019 Week 2a Date
7/29
SINGLE - ROW SUBQUERIES
Operator Meaning
= Equal to
> Greater than
>= Greater than or equal to
< Less than
8/9/2019 Week 2a Date
8/29
SELECT empno,ename,job
FROM empWHERE job=
( SELECT job FROM emp
WHERE empno=7521 );
Display the employees whose job is the same as that employee 7521.
8/9/2019 Week 2a Date
9/29
SELECT empno,ename,job,sal
FROM empWHERE job=
( SELECT job FROM emp
WHERE empno=7521 )
AND sal>( SELECT salFROM emp
WHERE empno=7654 )
Display the employees whose job is the same as that employee 7521 and
Whose salary is greater than that employee 7654.
>1250 TRUE
>1250 FALSE
>1250 FALSE
>1250 TRUE
8/9/2019 Week 2a Date
10/29
SELECT empno,ename, job,sal
FROM emp
WHERE sal=
( SELECT min(sal) FROM emp )
Using Group Function in a Subquery
Displays the employee number, name, job and salary of all employees whose
salary is equal to the minimum salary.
8/9/2019 Week 2a Date
11/29
The HAVING clause with subqueries
SELECT deptno,min(sal)
FROM emp
GROUP BY deptnoHAVING min(sal) >
(SELECT min(sal)
FROM emp
WHERE deptno=20 )
Displays all departments that have a minimum salary greater than that of
department 20.
8/9/2019 Week 2a Date
12/29
SELECT job,avg(sal)
FROM emp
GROUP BY jobHAVING avg(sal) =
(SELECT min(avg(sal))
FROM emp
GROUP BY job )
Find the job with the lowest average salary.
8/9/2019 Week 2a Date
13/29
SELECT empno,ename,sal
FROM emp
WHERE sal =
ERROR at line 3 :ORA-01427: Single-row subquery returns more than one row.
What is Wrong with This Statement ?
( SELECT MIN(sal)FROM emp
GROUP BY deptno )
One common error with subqueries is more than one row returned for single-row subquery.
Subquery contain GROUP BY clause, which implies that the subquery will return multiple rows,
One for each group it finds.In this case , the results of subquery will be 950, 800, and 1300.
The main query takes the result of subquery (950,800,1300) and uses these results in its WHERE
clause.
WHERE clause contain an uqual = operator, a single-row comparison operator expecting only
one value.
8/9/2019 Week 2a Date
14/29
SELECT empno,ename,sal
FROM emp
WHERE sal in
How Can You Correct This Statement ?
( SELECT MIN(sal)FROM emp
GROUP BY deptno )
To correct this error , change the = operator to IN
8/9/2019 Week 2a Date
15/29
Multiple-Row Subquery
Operator Meaning
IN Equal to any member in the list
ANY Compare value to each value returned by the subquery.
ALL Compare value to every value returned by the subquery.
Returns more than one row
Use multiple row comparison operators.
Fi d h l h h l h i i l f h
8/9/2019 Week 2a Date
16/29
SELECT empno,ename,sal
FROM empWHERE sal in
( SELECT MIN(sal)
FROM emp
GROUP BY deptno )
Find the employees who earn the same salary as the minimum salary for each
departments.
( 950 , 800 , 1300 )
The inner query executed first,
producing a query result.Main query uses that results.
In fact, the main query would look
like the following.
SELECT empno,ename,sal
FROM empWHERE sal in (950,800,1300)
U i th ANY O t i M lti l R S b i
8/9/2019 Week 2a Date
17/29
Using the ANY Operator in Multiple-Row Subqueries
SELECT empno,ename,job,sal
FROM emp
WHERE job SALESMAN
AND
sal < ANY ( SELECT sal
FROM emp
WHERE job=SALESMAN
The ANY operator compares a value to each value returned by a subquery.
Find employees who are not SALESMAN and whose salary is less than
that of any SALESMAN
< ANY means less than the maximum > ANY means more than the minimum
SELECT empno,ename,job,sal
FROM emp
WHERE jobSALESMAN
AND
sal < ANY ( SELECT sal FROM emp
WHERE job=SALESMAN )
= ANY is equivalent to IN
SELECT empno,ename,job,sal
FROM emp
WHERE jobSALESMAN
AND
sal < ( SELECT MAX(sal) FROM emp
WHERE job=SALESMAN )
SAME
8/9/2019 Week 2a Date
18/29
SAL
8/9/2019 Week 2a Date
19/29
Using the ALL Operator in Multiple-Row Subqueries
SELECT empno,ename,job,sal
FROM emp
WHERE job SALESMAN
AND
sal < ALL ( SELECT sal
FROM emp
WHERE job=SALESMAN
The ANY operator compares a value to everyvalue returned by a subquery.
Find employees who are not SALESMAN and whose salary is less than
that of all SALESMAN
< ALL means less than the minimum > ALL means more than the maximum
SELECT empno,ename,job,sal
FROM emp
WHERE jobSALESMAN
AND
sal < ANY ( SELECT sal FROM emp
WHERE job=SALESMAN )
The NOT operator can be used with IN, ANY and ALL operators
SELECT empno,ename,job,sal
FROM emp
WHERE jobSALESMAN
AND
sal < ( SELECT MAX(sal) FROM emp
WHERE job=SALESMAN )
SAME
8/9/2019 Week 2a Date
20/29
SAL
8/9/2019 Week 2a Date
21/29
SELECT empno,ename,job,sal
FROM emp
WHERE job SALESMAN AND
SAL < ANY ( SELECT sal FROM emp
WHERE job=SALESMAN
SELECT empno,ename,job,sal
FROM emp
WHERE job SALESMAN AND
SAL < ALL ( SELECT sal FROM emp
WHERE job=SALESMAN
SALESMANS SALARIES1250
1500
1600
1400
ANY ALL
1300 is less than all but not
from 1250.
8/9/2019 Week 2a Date
22/29
SELECT ENAME,SAL FROM EMP
WHERE SAL< ANY(SELECT MIN(SAL) FROM EMP GROUP BY DEPTNO)
EMP
1300