09 Nested Queries
-
Upload
nathan-wilson -
Category
Documents
-
view
212 -
download
0
Transcript of 09 Nested Queries
-
1
CS 338: Computer Applications in Business: Databases (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
Nested Queries
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.) Rice University Data Center
Fall 2014
Chapter 5
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
Nested Queries
2
It is often necessary to process data based on other processed data
-
2
CS 338: Computer Applications in Business: Databases (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 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
3
Nested Queries Produce Scalar Values: Example 1
4
Method 1: Step 1: SELECT Position FROM Employee WHERE Name = Victor;
Output: Manager
Step 2: SELECT * FROM Employee WHERE Position = Manager; Output:
111 John Sales Manager 444 Victor Marketing Manager 555 Mark Purchase Manager
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
Find all employees who have the same position as Victor
-
3
CS 338: Computer Applications in Business: Databases (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 Produce Scalar Values: Example 2
5
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
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
WHERE Name = Victor);
outer query inner query
Nested Queries Conditions Involving Relations
6
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. EXISTS R is a condition that is true if and only if R is not empty 2. 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
-
4
CS 338: Computer Applications in Business: Databases (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 Using IN Operator: Example
7
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
Find all employees with non-managerial positions
SELECT * FROM Employee WHERE Position IN ( SELECT Position FROM Employee WHERE Position NOT LIKE Manager%);
IN equivalent to = ANY
Nested Queries Using ALL Operator: Example
8
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
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);
>= ALL equivalent to = (SELECT MAX() )
-
5
CS 338: Computer Applications in Business: Databases (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 Using ANY Operator: Example
9
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
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
10
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 FROM Employee WHERE Name = Sue ) OR Salary >= ( SELECT Salary FROM Employee WHERE Name = Mary );
-
6
CS 338: Computer Applications in Business: Databases (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.)
Correlated Nested Queries
11
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
SELECT * FROM Employee as E1 WHERE Salary > ( SELECT AVG(Salary) FROM Employee as E2 WHERE E1.Dept = E2.Dept )
Find all employees who earn more than the average salary in their own department
EXISTS and UNIQUE Function in SQL
12
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
-
7
CS 338: Computer Applications in Business: Databases (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.)
EXISTS Function: Example
13
Example
SELECT * FROM Employee as E WHERE EXISTS ( SELECT * FROM Department as D WHERE E.DeptID = D.DeptID AND D.DeptName = Sales );
Find all sales employees
Employee
EmployeeID Name DeptID Position Salary
111 Sue 1 Manager 65000
222 John 2 Accountant 42000
333 Mary 1 Clerk 28000
444 Victor 3 Manager 52000
555 Mark 2 Manager 52000
666 Tony 3 Assistant Manager 38000
DeptID DeptName Location
1 Sales Room 245
2 Purchase Room 430
3 Marketing Room 212
Department
NOT EXISTS Function: Example
14
Example
SELECT * FROM Employee as E WHERE NOT EXISTS ( SELECT * FROM Department as D WHERE E.DeptID = D.DeptID AND D.DeptName = Sales);
Find all non-sales employees
Employee
EmployeeID Name DeptID Position Salary
111 Sue 1 Manager 65000
222 John 2 Accountant 42000
333 Mary 1 Clerk 28000
444 Victor 3 Manager 52000
555 Mark 2 Manager 52000
666 Tony 3 Assistant Manager 38000
DeptID DeptName Location
1 Sales Room 245
2 Purchase Room 430
3 Marketing Room 212
Department
-
8
CS 338: Computer Applications in Business: Databases (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 in FROM Clauses
15
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
SELECT * FROM ( SELECT * FROM Employee as E WHERE Salary > 45000 ) as SalariedEmployees WHERE Dept = Sales ;
Find all the employees earning more than 45000 and work for the Sales department