SQL-6 Subquery F22
SQL-6 Subquery F22
Subqueries
Asif Sohail
University of the Punjab
Punjab University College of Information Technology (PUCIT)
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 1
Objectives
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 2
Using a Subquery
to Solve a Problem
Main Query
Subquery
?
“What is Jones’ salary?”
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 3
Subqueries
• Query within a query is called subquery.
SELECT select_list
FROM table
WHERE expr operator
(SELECT select_list
FROM table);
• The subquery (inner query) executes once before the main
query.
• The result of the subquery is used by the main query (outer
query).
• Bottom-up or inward-outward parsing
• The subquery can also be paced in the HAVING, FROM clause
of the outer query.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 4
Using a Subquery
SQL> SELECT ename
2 FROM emp 2975
3 WHERE sal >
4 (SELECT sal
5 FROM emp
6 WHERE empno=7566);
ENAME
ENAME
----------
----------
KING
KING
FORD
FORD
SCOTT
SCOTT
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 5
Exercise
• List all employees with salary greater than the average
salary of the employees.
• List all employees with salary greater than the average
salary of the employees of a certain job/department.
• Find the name of the highest paid employee.
• Find the name of the most senior employee.
• List all employees with the same job/department as that
of a given employee.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 6
Types of Subqueries
1. Single-row subquery
•Returns zero on one row to the outer SQL statement. A
special case called scalar subquery contains exactly one
column.
2. Multiple-row subquery
•Returns one or more rows to the outer SQL statement.
•In addition, there are three subtypes of subqueries that may
return single or multiple rows:
a)Multiple-column subqueries return more than one column
to the outer SQL statement.
b)Correlated subqueries reference one or more columns in
the outer SQL statement.
c)Nested subqueries are placed within another subquery.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 7
Types of Subqueries
• Single-row subquery
Main query
returns
Subquery CLERK
• Multiple-row subquery
Main query
Subquery
returns CLERK
MANAGER
• Multiple-column subquery
Main query
returns
Subquery CLERK 7900
MANAGER 7698
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 8
Guidelines for Using Subqueries
• Enclose subqueries in parentheses.
• Place subqueries on the right side of the
comparison operator.
• ORDER BY clause is mostly not required in a
subquery.
• Use single-row operators with single-row
subqueries.
• Use multiple-row operators (IN,ANY,ALL) with
multiple-row subqueries.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 9
Single-Row Subqueries
– Return only one row
– Use single-row comparison operators
Operator Meaning
= Equal to
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 10
Executing Single-Row Subqueries
ENAME
ENAME JOB
JOB
----------
---------- ---------
---------
MILLER
MILLER CLERK
CLERK
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 11
Using Group Functions
in a Subquery (Scalar Subquery)
– Find the lowest paid employee
ENAME
ENAME JOB
JOB SAL
SAL
----------
---------- ---------
--------- ---------
---------
SMITH
SMITH CLERK
CLERK 800
800
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 12
Finding second highest sal
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 13
HAVING Clause with Subqueries
– The Oracle Server executes subqueries first.
– The Oracle Server returns results into the
HAVING clause of the main query.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 14
HAVING Clause with Subqueries
• List job, average salary of those jobs whose
average salary is greater than the minimum
average of different jobs.
SELECT JOB, AVG(SAL) FROM EMP
GROUP BY JOB
HAVING AVG(SAL) >
(SELECT MIN(AVG(SAL)) FROM EMP
GROUP BY JOB)
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 15
What Is Wrong
with This Statement?
no
no rows
rows selected
selected
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 16
Will This Statement Work?
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 17
Multiple-Row Subqueries
– Return more than one row
– Use multiple-row comparison operators
Operator Meaning
EMPNO
EMPNO ENAME
ENAME JOB
JOB
---------
--------- ----------
---------- ---------
---------
7369
7369 SMITH
SMITH CLERK
CLERK
7900
7900 JAMES
JAMES CLERK
CLERK
7934
7934 MILLER
MILLER CLERK
CLERK
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 19
Using ANY Operator
in Multiple-Row Subqueries
EMPNO
EMPNO ENAME
ENAME JOB
JOB
---------
--------- ----------
---------- ---------
---------
7654
7654 MARTIN
MARTIN SALESMAN
SALESMAN
7521
7521 WARD
WARD SALESMAN
SALESMAN
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 20
Using ALL Operator
in Multiple-Row Subqueries
EMPNO
EMPNO ENAME
ENAME JOB
JOB
---------
--------- ----------
---------- ---------
---------
7839
7839 KING
KING PRESIDENT
PRESIDENT
7566
7566 JONES
JONES MANAGER
MANAGER
7902
7902 FORD
FORD ANALYST
ANALYST
7788
7788 SCOTT
SCOTT ANALYST
ANALYST
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 21
Null Values in a Subquery
List all employees who do not have any subordinates.
SQL> SELECT e.ename
2 FROM emp e
3 WHERE e.empno NOT IN
4 (SELECT manager.mgr
5 FROM emp manager);
no rows selected.
ENAME JOB
SMITH CLERK
ALLEN SALESMAN
WARD SALESMAN
MARTIN SALESMAN
TURNER SALESMAN
ADAMS CLERK
JAMES CLERK
MILLER CLERK
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 23
Multiple-Column Subqueries
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 24
Multiple-Column Subqueries
Main query
MANAGER 10
Subquery
SALESMAN 30
MANAGER 10
CLERK 20
MANAGER 10 SALESMAN 30
MANAGER 10
CLERK 20
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 25
Using Multiple-Column Subqueries
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 26
Column Comparisons
Pairwise Nonpairwise
PRODID QTY PRODID QTY
101863 100 101863 100
100861 100 100861 100
102130 10 102130 10
100890 5 100890 5
100870 500 100870 500
101860 50 101860 50
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 27
Nonpairwise Comparison Subquery
• Display the order number, product number, and
quantity of any item in which the product number
and quantity match any product number and any
quantity of an item in order 605.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 28
Nonpairwise Subquery
ORDID PRODID QTY
--------- --------- ---------
609 100870 5
616 100861 10
616 102130 10
621 100861 10
618 100870 10
618 100861 50
616 100870 50
617 100861 100
619 102130 100
615 100870 100
617 101860 100
621 100870 100
617 102130 100
. . .
16 rows selected.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 29
Main query
references
Correlated Subquery Subquery
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 30
Correlated Subquery
– The EXISTS operator checks for the existence of a row
in the subquery.
– EXISTS typically offers better performance than IN.
– When a list of values contains a NULL value, NOT
EXISTS return TRUE, but NOT IN returns FALSE.
– List all employees who do not have any subordinates.
SELECT e.ename,e.job FROM emp e ENAME JOB
WHERE NOT EXISTS SMITH CLERK
(SELECT empno FROM emp m
ALLEN SALESMAN
WHERE m.mgr = e.empno);
WARD SALESMAN
MARTIN SALESMAN
TURNER SALESMAN
ADAMS CLERK
JAMES CLERK
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 MILLER CLERK Slide 31
Correlated Subquery
• Finding an employee with Nth highest salary
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 32
Main query
references
Correlated Subquery Subquery
SELECT ename,job,sal,
(SELECT ROUND(AVG(sal))from emp e2
WHERE e1.job=e2.job)"avg-job-sal"
FROM emp e1
ENAME
ENAME SAL
SAL DEPTNO
DEPTNO SALAVG
SALAVG
----------
---------- ---------
--------- ---------
--------- ----------
----------
KING
KING 5000
5000 10
10 2916.6667
2916.6667
JONES
JONES 2975
2975 20
20 2175
2175
SCOTT
SCOTT 3000
3000 20
20 2175
2175
...
...
66 rows
rows selected.
selected.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 35
Exercise
• Find employees who work for the department in which employee ID 7369 is
employed.
• List all employees with salary less than the average salary of all the employees.
• List all employees with salary less than the average salary of any of the
departments.
• Find those employees whose salary matches the lowest salary of any of the
departments.
• List all employees with salary less than the average salary of their respective
departments.
• List all the departments with no employees.
• Find all those departments where at least one employee is employed.
•
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 36
Exercise
• List all employees with salary greater than the average
salary of the respective departments.
select ename, job, sal,
(select avg(sal) from emp e
Where outer.deptno=e.deptno) "avg-sal"
from emp outer
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 37
Thank you for your attention.
Asif Sohail
Assistant Professor
University of the Punjab
Punjab University College of Information Technology (PUCIT)
Allama Iqbal (Old) Campus, Anarkali
Lahore, Pakistan
Tel: +92-(0)42-111-923-923 Ext. 154
E-mail: [email protected]
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 38