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);
ENAME
----------
KING
FORD
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
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 7
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 8
Types of Subqueries
• Single-row subquery
Main query
returns
Subquery CLERK
• Multiple-row subquery
Main query
returns CLERK
Subquery
MANAGER
• Multiple-column subquery
Main query
returns
Subquery CLERK 7900
MANAGER 7698
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 9
Guidelines for Using Subqueries
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 10
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 11
Executing Single-Row Subqueries
ENAME JOB
---------- ---------
MILLER CLERK
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 12
Using Group Functions
in a Subquery (Scalar Subquery)
– Find the lowest paid employee
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 13
Finding second highest sal
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 14
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 15
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 16
What Is Wrong
with This Statement?
ERROR:
ORA-01427: single-row subquery returns more than
one row
no rows selected
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 17
Will This Statement Work?
no rows selected
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 18
Multiple-Row Subqueries
– Return more than one row
– Use multiple-row comparison operators
Operator Meaning
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 20
Using ANY Operator
in Multiple-Row Subqueries
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 21
Using ALL Operator
in Multiple-Row Subqueries
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 22
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.
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 24
Multiple-Column Subqueries
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 25
Multiple-Column Subqueries
Main query
MANAGER 10
Subquery
SALESMAN 30
MANAGER 10
CLERK 20
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 26
Using Multiple-Column Subqueries
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 27
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 28
Nonpairwise Comparison Subquery
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 29
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 30
Main query
references
Correlated Subquery Subquery
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 31
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
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 34
Correlated Subquery
• Finding a department without an employee
SELECT * from dept
WHERE NOT EXISTS
(SELECT * from emp where emp.deptno=dept.deptno)
© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 35
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 36