SlideShare a Scribd company logo
2
Most read
Subqueries
CIS-182
Subqueries
• A subquery is one SELECT statement
inside a second SELECT statement
– May be used throughout the main query, in
SELECT, FROM, WHERE or HAVING
– Parentheses control order of execution
Sample Subqueries
• Display the price and average price of all
books:
SELECT Price, (SELECT Avg(Price) FROM
titles) AS AveragePrice FROM titles
• Display all books with a higher than
average price:
SELECT title FROM titles WHERE
Price>(SELECT Avg(Price) FROM titles)
Subquery Results
• Scalar values: Subqueries may return a
single value
• Lists: Subqueries may return one or more
rows
– Some situations require a single column list
– Typically use IN or EXISTS to test
Scalar Subqueries Example
• Display titles that have the highest price:
SELECT title, price
FROM titles
WHERE Price = (SELECT Max(price) FROM
Titles)
List Subqueries Example
• Display the publishers who have
published cook books:
SELECT pub_name
FROM publishers
WHERE pub_id IN
(SELECT pub_id FROM titles WHERE [type]
LIKE ‘%cook%’)
Correlated Subqueries
• A correlated subquery uses a value from
the main query as part of the inner query
– Data from each row in the main query is
“passed” to the subquery for processing
• Typically a processing-intensive operation
– Subquery must be re-run with new value(s)
for each row
Correlated Subquery Example
• Display all books with a higher than
average price for that type of book
SELECT title
FROM titles t1
WHERE price >
(SELECT Avg(price) FROM titles t2 WHERE
t1.type = t2.type)
Using Subqueries - 1
• If data that’s known is from one table and
data to return is in a second table
• Display authors who have written books
(titleauthors represents what’s known):
SELECT au_fname, au_lname
FROM authors
WHERE au_id IN
(SELECT au_id FROM titleauthors)
Using Subqueries – 2
• If data to return depends on a calculation
from a related set
• Display books where actual sales have
exceeded projected sales:
SELECT title
FROM titles t
WHERE projected_sales <
(SELECT sum(qty_shipped) FROM salesdetails
sd WHERE t.title_id=sd.title_id)

More Related Content

PPT
SQL subquery
PDF
Sub query_SQL
 
PPTX
SQL Functions
PPT
MYSQL Aggregate Functions
PPT
Sql join
PPTX
Sql subquery
PPTX
advanced sql(database)
PPT
Using the set operators
SQL subquery
Sub query_SQL
 
SQL Functions
MYSQL Aggregate Functions
Sql join
Sql subquery
advanced sql(database)
Using the set operators

What's hot (20)

PDF
[APJ] Common Table Expressions (CTEs) in SQL
 
PDF
PL/SQL TRIGGERS
PPT
Introduction to-sql
PPTX
SQL Commands
DOC
PDF
SQL Joins With Examples | Edureka
PPT
Advanced sql
PPTX
Integrity Constraints
PPT
Advanced Sql Training
PPTX
DATABASE CONSTRAINTS
PPTX
set operators.pptx
PPTX
SQL JOIN
PPT
Mysql
PDF
SQL Queries - DDL Commands
PPTX
Sql queries presentation
PPTX
Aggregate function
PPT
SQL Queries
PPTX
SQL UNION
PPT
SQL DDL
[APJ] Common Table Expressions (CTEs) in SQL
 
PL/SQL TRIGGERS
Introduction to-sql
SQL Commands
SQL Joins With Examples | Edureka
Advanced sql
Integrity Constraints
Advanced Sql Training
DATABASE CONSTRAINTS
set operators.pptx
SQL JOIN
Mysql
SQL Queries - DDL Commands
Sql queries presentation
Aggregate function
SQL Queries
SQL UNION
SQL DDL
Ad

Viewers also liked (20)

PDF
Nested Queries Lecture
PPT
Subqueries -Oracle DataBase
PDF
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
PDF
Sql subquery
PPT
Sql joins
DOC
Sql queries with answers
PPT
1 - Introduction to PL/SQL
PPT
PL/SQL Introduction and Concepts
PPT
e computer notes - Subqueries
PPTX
Subqueries, Backups, Users and Privileges
PPTX
Black hat hackers
PPTX
PL/SQL Fundamentals I
PPTX
ORACLE PL SQL FOR BEGINNERS
PPT
SQL Views
PPTX
Oracle: PLSQL Introduction
DOC
Best sql plsql material
PPT
SQL212.2 Introduction to SQL using Oracle Module 2
PPT
SQL : introduction
PPTX
SQL Basics
PPT
SQL Tutorial - Basic Commands
Nested Queries Lecture
Subqueries -Oracle DataBase
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Sql subquery
Sql joins
Sql queries with answers
1 - Introduction to PL/SQL
PL/SQL Introduction and Concepts
e computer notes - Subqueries
Subqueries, Backups, Users and Privileges
Black hat hackers
PL/SQL Fundamentals I
ORACLE PL SQL FOR BEGINNERS
SQL Views
Oracle: PLSQL Introduction
Best sql plsql material
SQL212.2 Introduction to SQL using Oracle Module 2
SQL : introduction
SQL Basics
SQL Tutorial - Basic Commands
Ad

Similar to Subqueries (8)

PPTX
Analytics on Spreadsheets-converted.pptx
PPTX
Server Query Language – Getting Started.pptx
PPTX
4. chapter iv(transform)
PDF
From 0 to DAX…………………………………………………………..pdf
PPTX
normalisation jdsuhduswwhdusw cdscsacasc.pptx
PDF
Simple Spreadsheet Tips
Analytics on Spreadsheets-converted.pptx
Server Query Language – Getting Started.pptx
4. chapter iv(transform)
From 0 to DAX…………………………………………………………..pdf
normalisation jdsuhduswwhdusw cdscsacasc.pptx
Simple Spreadsheet Tips

More from Randy Riness @ South Puget Sound Community College (20)

Recently uploaded (20)

PDF
How to Get Funding for Your Trucking Business
PPTX
Sales & Distribution Management , LOGISTICS, Distribution, Sales Managers
PPTX
DMT - Profile Brief About Business .pptx
PDF
Tata consultancy services case study shri Sharda college, basrur
PDF
NewBase 12 August 2025 Energy News issue - 1812 by Khaled Al Awadi_compresse...
PDF
SIMNET Inc – 2023’s Most Trusted IT Services & Solution Provider
PDF
NEW - FEES STRUCTURES (01-july-2024).pdf
PDF
Charisse Litchman: A Maverick Making Neurological Care More Accessible
PDF
Module 2 - Modern Supervison Challenges - Student Resource.pdf
PPTX
Business Ethics - An introduction and its overview.pptx
PPTX
Belch_12e_PPT_Ch18_Accessible_university.pptx
PDF
Outsourced Audit & Assurance in USA Why Globus Finanza is Your Trusted Choice
PDF
Family Law: The Role of Communication in Mediation (www.kiu.ac.ug)
PPTX
svnfcksanfskjcsnvvjknsnvsdscnsncxasxa saccacxsax
PDF
Keppel_Proposed Divestment of M1 Limited
PDF
Deliverable file - Regulatory guideline analysis.pdf
PDF
TyAnn Osborn: A Visionary Leader Shaping Corporate Workforce Dynamics
PDF
Solaris Resources Presentation - Corporate August 2025.pdf
PPT
Lecture 3344;;,,(,(((((((((((((((((((((((
PDF
Ôn tập tiếng anh trong kinh doanh nâng cao
How to Get Funding for Your Trucking Business
Sales & Distribution Management , LOGISTICS, Distribution, Sales Managers
DMT - Profile Brief About Business .pptx
Tata consultancy services case study shri Sharda college, basrur
NewBase 12 August 2025 Energy News issue - 1812 by Khaled Al Awadi_compresse...
SIMNET Inc – 2023’s Most Trusted IT Services & Solution Provider
NEW - FEES STRUCTURES (01-july-2024).pdf
Charisse Litchman: A Maverick Making Neurological Care More Accessible
Module 2 - Modern Supervison Challenges - Student Resource.pdf
Business Ethics - An introduction and its overview.pptx
Belch_12e_PPT_Ch18_Accessible_university.pptx
Outsourced Audit & Assurance in USA Why Globus Finanza is Your Trusted Choice
Family Law: The Role of Communication in Mediation (www.kiu.ac.ug)
svnfcksanfskjcsnvvjknsnvsdscnsncxasxa saccacxsax
Keppel_Proposed Divestment of M1 Limited
Deliverable file - Regulatory guideline analysis.pdf
TyAnn Osborn: A Visionary Leader Shaping Corporate Workforce Dynamics
Solaris Resources Presentation - Corporate August 2025.pdf
Lecture 3344;;,,(,(((((((((((((((((((((((
Ôn tập tiếng anh trong kinh doanh nâng cao

Subqueries

  • 2. Subqueries • A subquery is one SELECT statement inside a second SELECT statement – May be used throughout the main query, in SELECT, FROM, WHERE or HAVING – Parentheses control order of execution
  • 3. Sample Subqueries • Display the price and average price of all books: SELECT Price, (SELECT Avg(Price) FROM titles) AS AveragePrice FROM titles • Display all books with a higher than average price: SELECT title FROM titles WHERE Price>(SELECT Avg(Price) FROM titles)
  • 4. Subquery Results • Scalar values: Subqueries may return a single value • Lists: Subqueries may return one or more rows – Some situations require a single column list – Typically use IN or EXISTS to test
  • 5. Scalar Subqueries Example • Display titles that have the highest price: SELECT title, price FROM titles WHERE Price = (SELECT Max(price) FROM Titles)
  • 6. List Subqueries Example • Display the publishers who have published cook books: SELECT pub_name FROM publishers WHERE pub_id IN (SELECT pub_id FROM titles WHERE [type] LIKE ‘%cook%’)
  • 7. Correlated Subqueries • A correlated subquery uses a value from the main query as part of the inner query – Data from each row in the main query is “passed” to the subquery for processing • Typically a processing-intensive operation – Subquery must be re-run with new value(s) for each row
  • 8. Correlated Subquery Example • Display all books with a higher than average price for that type of book SELECT title FROM titles t1 WHERE price > (SELECT Avg(price) FROM titles t2 WHERE t1.type = t2.type)
  • 9. Using Subqueries - 1 • If data that’s known is from one table and data to return is in a second table • Display authors who have written books (titleauthors represents what’s known): SELECT au_fname, au_lname FROM authors WHERE au_id IN (SELECT au_id FROM titleauthors)
  • 10. Using Subqueries – 2 • If data to return depends on a calculation from a related set • Display books where actual sales have exceeded projected sales: SELECT title FROM titles t WHERE projected_sales < (SELECT sum(qty_shipped) FROM salesdetails sd WHERE t.title_id=sd.title_id)