Anda di halaman 1dari 8

CS 338: Computer Applications in Business: Databases (Fall 2014)

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.)

CS 338: Computer Applications in Business: Databases


Rice University Data Center

Nested Queries
It is often necessary to process data based on other processed data

Some queries require that existing values in the database be


fetched and then used in a comparison condition
Nested queries: complete select-from-where blocks within WHERE clause of
another query
Nested queries are also called subqueries, or inner queries
Outer query is the remaining part of the query

There are a number of ways a nested query can be used:


1. Nested queries can return a single constant, and this constant can be
compared with another value in a WHERE clause
2. Nested queries can return relations that can be used in various ways in
WHERE clauses
3. Nested queries can appear in FROM clauses, followed by a tuple variable
representing the tuples in the result of the nested query
2

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.)

CS 338: Computer Applications in Business: Databases (Fall 2014)

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

Find all employees who have the same position as Victor


Method 1:
Step 1: SELECT Position FROM Employee WHERE Name = Victor;
Output: Manager

Step 2: SELECT * FROM Employee WHERE Position = Manager;


Output:

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.)

CS 338: Computer Applications in Business: Databases (Fall 2014)

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

Find all employees who have the same position as Victor


Method 2: Use Nested Query or Subquery
SELECT *
FROM Employee
WHERE Position = (

SELECT Position
FROM Employee

outer query

WHERE Name = Victor);

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

Operators: IN, ALL, and ANY


1.
2.

EXISTS R is a condition that is true if and only if R is not empty


Scalar value IN R is true if and only if scalar value is equal to one of the
values in R (compares value v with a set of values V)
Likewise, scalar value NOT IN R is true if and only if scalar value is equal to no value
in R

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.)

CS 338: Computer Applications in Business: Databases (Fall 2014)

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

Find all employees with non-managerial positions


SELECT *
FROM Employee
WHERE Position IN (

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

>= ALL equivalent to


= (SELECT MAX() )

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.)

CS 338: Computer Applications in Business: Databases (Fall 2014)

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

Multiple Nested Queries


A WHERE clause of a query may contain one or more
subqueries combined using operators AND or OR
Example

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 )

Salary >= ( SELECT Salary


FROM Employee
WHERE Name = Mary );

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.)

CS 338: Computer Applications in Business: Databases (Fall 2014)

Correlated Nested Queries


Correlated nested query

Inner query refers to a table from the outer query


Inner query evaluated once for each tuple in the outer query

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

EXISTS and UNIQUE Function in SQL


EXISTS function
Check whether the result of a correlated nested query is empty or not
Produces simple TRUE or FALSE result

TRUE if the nested query result contains at least one row


FALSE if nested query result contains no rows

NOT EXISTS is the opposite of EXISTS

EXISTS and NOT EXISTS

Typically used in conjunction with a correlated nested query

UNIQUE(Q)function

Returns TRUE if there are no duplicate tuples in the result of query Q

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.)

CS 338: Computer Applications in Business: Databases (Fall 2014)

EXISTS Function: Example


Employee
EmployeeID

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

Find all sales employees

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

NOT EXISTS Function: Example


Employee
EmployeeID

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

Find all non-sales employees

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.)

CS 338: Computer Applications in Business: Databases (Fall 2014)

Nested Queries in FROM Clauses


In a FROM list, instead of a stored relation, we may use a
parenthesized nested query

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.)

Anda mungkin juga menyukai