Module 6: Working with
Subqueries
Overview
Introduction to Subqueries
Using a Subquery as a Derived Table
Using a Subquery as an Expression
Using a Subquery to Correlate Data
Using the EXISTS and NOT EXISTS
Clauses
Introduction to Subqueries
Why to Use Subqueries
To break down a complex query into a
series of
logical steps
To answer a query that relies on the
results of an
other query
Why to Use Joins Rather Than
Subqueries
SQL Server executes joins faster than
subqueries
Using a Subquery as a Derived Table
Is a Recordset Within a Query That
Functions as a Table
Takes the Place of a Table in the
FROM Clause
Is Optimized with the Rest of the
USE Query
northwind
SELECT T.orderid, T.customerid
FROM ( SELECT orderid, customerid
FROM orders ) AS T
GO
Using a Subquery as an Expression
Is Evaluated and Treated as an
Expression
Is Executed Once for the Query
USE pubs
SELECT title, price
,( SELECT AVG(price) FROM titles) AS average
,price-(SELECT AVG(price) FROM titles) AS difference
FROM titles
WHERE type='popular_comp'
GO
Using a Subquery to Correlate
Data
Evaluating a Correlated Subquery
Mimicking a JOIN Clause
Mimicking a HAVING Clause
Evaluating a Correlated Subquery
Outer query passes column
values to the inner query
USE northwind Inner query uses that value to
SELECT orderid, customerid satisfy the inner query
FROM orders AS or1
WHERE 20 < (SELECT quantity
FROM [order details] AS od
WHERE or1.orderid = od.orderid Example 1
AND od.productid = 23)
GO
Inner query returns a value
back to the outer query The process is repeated for the
next row of the outer query
Back to Step 1
Mimicking a JOIN Clause
Correlated Subqueries Can Produce
the Same Result as a JOIN Clause
Joins Let the Query Optimizer
Determine How to Correlate Data Most
Efficiently
Example 1
USE pubs
SELECT DISTINCT t1.type
FROM titles AS t1
WHERE t1.type IN
(SELECT t2.type
FROM titles AS t2
WHERE t1.pub_id <> t2.pub_id)
GO
Mimicking a HAVING Clause
Subquery with the Same Result As a
HAVING Clause
USE pubs Example 1
SELECT t1.type, t1.title, t1.price
FROM titles AS t1
WHERE t1.price > ( SELECT AVG(t2.price) FROM titles AS t2
WHERE t1.type = t2.type )
GO
Using a HAVING Clause Without aExample 2
USE pubs
Subquery
SELECT t1.type, t1.title, t1.price
FROM titles AS t1
INNER JOIN titles AS t2 ON t1.type = t2.type
GROUP BY t1.type, t1.title, t1.price
HAVING t1.price > AVG(t2.price)
GO
Using the EXISTS and NOT EXISTS
Clauses
Use with Correlated Subqueries
Determine Whether Data Exists in a
List of Values
SQL Server Process
Outer query tests for the existence of
rows
Inner query returns TRUE or FALSE
No data is produced
Example 1
USE northwind
SELECT lastname, employeeid
FROM employees AS e
WHERE EXISTS (SELECT * FROM orders AS o
WHERE e.employeeid = o.employeeid
AND o.orderdate = '9/5/97')
GO
Recommended Practices
Use Subqueries to Break Down a Complex Query
Use Table Name Aliases for Correlated Subqueries
Use the INSERT…SELECT Statement to Add Rows from
Other Sources to an Existing Table
Use the EXISTS Operator Instead of the IN Operator
Lab A: Working with Subqueries
Review
Introduction to Subqueries
Using a Subquery as a Derived Table
Using a Subquery as an Expression
Using a Subquery to Correlate Data
Using the EXISTS and NOT EXISTS
Clauses