Nested Queries
Chapter 5
Fall 2014
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ),
Database Systems: Complete Book (Garcia-Molina et al.)
Nested Queries
It is often necessary to process data based on other processed data
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
Nested Queries
Produce Scalar Values
An atomic value that can appear as one component of a tuple
is referred to as a scalar
A select-from-where expression can produce a relation with any
number of attributes in its schema, and there can be any number of
tuples in the relation
However, we are often interested in values of a single attribute.
If so, we can use this select-from-where expression surrounded by
parentheses, as if it were a constant
Nested Queries
Produce Scalar Values: Example 1
Employee
EmployeeID
Name
Dept
Position
Salary
111
Sue
Sales
Manager
65000
222
John
Purchase
Accountant
42000
333
Mary
Sales
Clerk
28000
444
Victor
Marketing
Manager
52000
555
Mark
Purchase
Manager
52000
666
Tony
Marketing
Assistant Manager
38000
Example 1
111
444
555
John
Victor
Mark
Sales
Marketing
Purchase
Manager
Manager
Manager
4
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
Nested Queries
Produce Scalar Values: Example 2
Employee
EmployeeID
Name
Dept
Position
Salary
111
Sue
Sales
Manager
65000
222
John
Purchase
Accountant
42000
333
Mary
Sales
Clerk
28000
444
Victor
Marketing
Manager
52000
555
Mark
Purchase
Manager
52000
666
Tony
Marketing
Assistant Manager
38000
Example 2
SELECT Position
FROM Employee
outer query
inner query
5
Nested Queries
Conditions Involving Relations
There are a number of SQL operators that we can apply to a relation R
and produce a Boolean result
Relation R must be expressed as a nested query or subquery
3.
Scalar value > ALL R is true if and only if scalar value is greater than
every value in unary relation R
The > operator could be replaced by any of the other comparison operators
4.
Scalar value > ANY R is true if and only if scalar value is greater than at
least one value in unary relation R
The > operator could be replaced by any of the other comparison operators
6
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
Nested Queries
Using IN Operator: Example
Employee
EmployeeID
Name
Dept
Position
Salary
111
Sue
Sales
Manager
65000
222
John
Purchase
Accountant
42000
333
Mary
Sales
Clerk
28000
444
Victor
Marketing
Manager
52000
555
Mark
Purchase
Manager
52000
666
Tony
Marketing
Assistant Manager
38000
Example
IN equivalent to
= ANY
SELECT Position
FROM Employee
WHERE Position NOT LIKE Manager%);
Nested Queries
Using ALL Operator: Example
Employee
EmployeeID
Name
Dept
Position
Salary
111
Sue
Sales
Manager
65000
222
John
Purchase
Accountant
42000
333
Mary
Sales
Clerk
28000
444
Victor
Marketing
Manager
52000
555
Mark
Purchase
Manager
52000
666
Tony
Marketing
Assistant Manager
38000
Example
Find all employees who earn more than all (every) employees in the Purchase
department
SELECT *
FROM Employee
WHERE Salary > ALL
SELECT Salary
FROM Employee
WHERE Dept = Purchase);
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
Nested Queries
Using ANY Operator: Example
Employee
EmployeeID
Name
Dept
Position
Salary
111
Sue
Sales
Manager
65000
222
John
Purchase
Accountant
42000
333
Mary
Sales
Clerk
28000
444
Victor
Marketing
Manager
52000
555
Mark
Purchase
Manager
52000
666
Tony
Marketing
Assistant Manager
38000
Example
Find all employees who earn more than any (or at least one) employee in the
Purchase department
SELECT *
FROM Employee
WHERE Salary > ANY
( SELECT Salary
FROM Employee
WHERE Dept = Purchase);
can use SOME instead of ANY
List all employees with either work in the same department as Sue or have a salary
same or greater than Mary
SELECT *
FROM Employee
WHERE Dept =
( SELECT Dept
OR
FROM Employee
WHERE Name = Sue )
10
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
Example
Find all employees who earn more than the average salary in their own department
SELECT *
FROM Employee as E1
WHERE Salary > ( SELECT AVG(Salary)
FROM Employee as E2
WHERE E1.Dept = E2.Dept )
11
UNIQUE(Q)function
12
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
Name
DeptID
Position
Salary
111
Sue
Manager
65000
222
John
Accountant
42000
333
Mary
Clerk
28000
444
Victor
Manager
52000
555
Mark
Manager
52000
666
Tony
Assistant Manager
38000
Department
Example
DeptID
DeptName
Location
Sales
Room 245
Purchase
Room 430
3
Marketing
Room 212
SELECT *
FROM
Employee as E
WHERE EXISTS ( SELECT *
FROM Department as D
WHERE E.DeptID = D.DeptID AND D.DeptName = Sales );
13
Name
DeptID
Position
Salary
111
Sue
Manager
65000
222
John
Accountant
42000
333
Mary
Clerk
28000
444
Victor
Manager
52000
555
Mark
Manager
52000
666
Tony
Assistant Manager
38000
Example
Department
DeptID
DeptName
Location
Sales
Room 245
Purchase
Room 430
Room 212
SELECT *
FROM Employee as E
WHERE NOT EXISTS ( SELECT *
FROM Department as D
WHERE E.DeptID = D.DeptID AND D.DeptName = Sales);
3
Marketing
14
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)
Because we do not have a name for the result of this nested query, we
must give it a tuple-variable alias
Example
Find all the employees earning more than 45000 and work for the Sales department
SELECT *
FROM ( SELECT *
FROM Employee as E
WHERE Salary > 45000 ) as SalariedEmployees
WHERE Dept = Sales ;
15
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System Concepts (5/6)
(Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. ), Database Systems: Complete Book
(Garcia-Molina et al.)