SlideShare a Scribd company logo
Chapter 9
Subqueries
Objectives
Describe the types of problems that
subqueries can solve
Define subqueries
List the types of subqueries
Write single-row and multiple-row
subqueries
Using a Subquery
to Solve a Problem
Who has a GPA higher than Owen’s?
Which students have a GPA higher than
Owen’s GPA?
Main Query
What is Owen’s GPA?
Subquery
?
Subqueries
The subquery (inner query) executes once
before the main query.
The result of the subquery is used by the
main query (outer query).
SELECT select_list
FROM table
WHERE expr operator
(SELECT select_list
FROM table);
Using a Subquery
“Who has a GPA higher than Owen’s?”
LastName
GPA
Lee 3.82
Tham 3.89
SELECT LastName, GPA
from Student
where GPA >
(Select GPA from Student
Where LastName = 'Owen');
3.34
Guidelines for Using Subqueries
Enclose subqueries in parentheses.
Place subqueries on the right side of the
comparison operator.
Do not add an ORDER BY clause to a
subquery.
Use single-row operators with single-row
subqueries.
Types of Subqueries
Single-row subquery
Multiple-row subquery
Main query
Subquery
returns
DIC
DIC
DCS
Main query
Subquery
returns
Single-Row Subqueries
Return only one row
Use single-row comparison operators
Operator
=
>
>=
<
<=
<>
Meaning
Equal to
Greater than
Greater than or equal to
Less than
Less than or equal to
Not equal to
Retrieving data using Single-Row Subqueries
SELECT LastName, CourseID
from Student
where CourseID = Select CourseID from Student
Where LastName = 'Lewis');
Who study the same course as Lewis?
OUTPUT:
LastName CourseID
Lewis DICT
Nicosia DICT
Maser DICT
DICT
Retrieving data using Single-Row Subqueries
SELECT LastName,GroupLeader
from Student
where GroupLeader = (Select GroupLeader from Student
Where LastName = 'Williams');
S010
Who has the same group leader as Williams?
OUTPUT:
LastName GroupLeader
Mikulski S010
Faga S010
Williams S010
Retrieving data using Single-Row Subqueries
SELECT LastName,DateEnrolled
from Student
where DateEnrolled < (Select DateEnrolled from Student
Where StudID = 'S009');
Who have been enrolled earlier than student S009?
01-Feb-02
OUTPUT: LastName DateEnrolled
Kebel 23-Jun-01
Lee 05-Jan-02
Lewis 03-Mar-00
Law 01-Apr-01
Faga 25-Jun-01
Owen 17-Sep-01
Ng 01-Apr-01
Roche 30-Mar-00
Jann 01-Apr-01
Retrieving data using Single-Row Subqueries
with GROUP functions
Display all the students that earn the minimum GPA
SELECT LastName,GPA
from Student
where GPA = (Select min(GPA)
from student);
1.88
OUTPUT:
LastName GPA
Ng 1.88
Roche 1.88
Multiple-Row Subqueries
Return more than one row
Use the IN multiple-row comparison
operator to compare an expression to any
member in the list that a subquery returns
Using Group Functions
in a Multiple-Row Subquery
Display all students who earn the same GPA as the
minimum GPA for each course
1.88,1.89,2.22,3
SELECT LastName,GPA, CourseID
from Student
where GPA IN
(select min(GPA)
from student
Group By CourseID);
OUTPUT: LastName GPA CourseID
Mikulski 1.89 DCS
Faga 2.22 DIC
Ng 1.88 DIT
Maser 3 DICT
Roche 1.88 DIT
Using Group Functions
in a Multiple-Row Subquery
Display all students who enrolled the same course as Law and Lewis.
DICT, DIT
SELECT LastName, CourseID
from Student
where CourseID IN
(select CourseID
from Student
where LastName IN ('Lewis','Law'))
order by CourseID;
OUTPUT:
LastName CourseID
Maser DICT
Nicosia DICT
Lewis DICT
Roche DIT
Ng DIT
Law DIT
Lee DIT
Summary
A subquery is a SELECT statement that is
embedded in a clause of another SQL
statement. Subqueries are useful when a query
is based on unknown criteria.
Subqueries have the following characteristics:
 Can pass one row of data to a main statement that
contains a single-row operator, such as =, <>, >, >=,
<, or <=
 Can pass multiple rows of data to a main statement
that contains a multiple-row operator, such as IN
 Can contain group functions

More Related Content

Similar to Chapter-9.ppt

Basics of Java
Basics of JavaBasics of Java
Basics of Java
Prarabdh Garg
 
Case 9 and 10. SQL For Data Analysis 4.pptx
Case 9 and 10. SQL For Data Analysis 4.pptxCase 9 and 10. SQL For Data Analysis 4.pptx
Case 9 and 10. SQL For Data Analysis 4.pptx
DilaTriarini1
 
SQL subquery
SQL subquerySQL subquery
SQL subquery
Vikas Gupta
 
Subconsultas
SubconsultasSubconsultas
Subconsultas
Maria
 
SQL Operators.pptx
SQL Operators.pptxSQL Operators.pptx
SQL Operators.pptx
ssuserb8d5cb
 
CIS160 final review
CIS160 final reviewCIS160 final review
SQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdfSQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdf
Madhusha15
 
Unit testing
Unit testingUnit testing
Unit testing
Pooya Sagharchiha
 
Md06 advance class features
Md06 advance class featuresMd06 advance class features
Md06 advance class features
Rakesh Madugula
 
SQL Operators.pptx
SQL Operators.pptxSQL Operators.pptx
SQL Operators.pptx
RUBAB79
 
Using subqueries to solve queries
Using subqueries to solve queriesUsing subqueries to solve queries
Using subqueries to solve queries
Syed Zaid Irshad
 
Input Space Partitioning
Input Space PartitioningInput Space Partitioning
Input Space Partitioning
Riyad Parvez
 
Sql subquery
Sql  subquerySql  subquery
Sql subquery
Raveena Thakur
 

Similar to Chapter-9.ppt (13)

Basics of Java
Basics of JavaBasics of Java
Basics of Java
 
Case 9 and 10. SQL For Data Analysis 4.pptx
Case 9 and 10. SQL For Data Analysis 4.pptxCase 9 and 10. SQL For Data Analysis 4.pptx
Case 9 and 10. SQL For Data Analysis 4.pptx
 
SQL subquery
SQL subquerySQL subquery
SQL subquery
 
Subconsultas
SubconsultasSubconsultas
Subconsultas
 
SQL Operators.pptx
SQL Operators.pptxSQL Operators.pptx
SQL Operators.pptx
 
CIS160 final review
CIS160 final reviewCIS160 final review
CIS160 final review
 
SQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdfSQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdf
 
Unit testing
Unit testingUnit testing
Unit testing
 
Md06 advance class features
Md06 advance class featuresMd06 advance class features
Md06 advance class features
 
SQL Operators.pptx
SQL Operators.pptxSQL Operators.pptx
SQL Operators.pptx
 
Using subqueries to solve queries
Using subqueries to solve queriesUsing subqueries to solve queries
Using subqueries to solve queries
 
Input Space Partitioning
Input Space PartitioningInput Space Partitioning
Input Space Partitioning
 
Sql subquery
Sql  subquerySql  subquery
Sql subquery
 

Recently uploaded

"Scaling RAG Applications to serve millions of users", Kevin Goedecke
"Scaling RAG Applications to serve millions of users",  Kevin Goedecke"Scaling RAG Applications to serve millions of users",  Kevin Goedecke
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
Fwdays
 
Christine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptxChristine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptx
christinelarrosa
 
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and BioinformaticiansBiomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Neo4j
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
The Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptxThe Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptx
operationspcvita
 
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptxPRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
christinelarrosa
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
DanBrown980551
 
Demystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through StorytellingDemystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through Storytelling
Enterprise Knowledge
 
Christine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptxChristine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptx
christinelarrosa
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving
 
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
saastr
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
Dandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity serverDandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity server
Antonios Katsarakis
 
Essentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation ParametersEssentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation Parameters
Safe Software
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin..."$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
Fwdays
 
"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
Fwdays
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
Javier Junquera
 
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
Alex Pruden
 
Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
c5vrf27qcz
 

Recently uploaded (20)

"Scaling RAG Applications to serve millions of users", Kevin Goedecke
"Scaling RAG Applications to serve millions of users",  Kevin Goedecke"Scaling RAG Applications to serve millions of users",  Kevin Goedecke
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
 
Christine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptxChristine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptx
 
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and BioinformaticiansBiomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
The Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptxThe Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptx
 
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptxPRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
 
Demystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through StorytellingDemystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through Storytelling
 
Christine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptxChristine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptx
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
 
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
Dandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity serverDandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity server
 
Essentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation ParametersEssentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation Parameters
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin..."$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
 
"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
 
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
 
Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
 

Chapter-9.ppt

  • 2. Objectives Describe the types of problems that subqueries can solve Define subqueries List the types of subqueries Write single-row and multiple-row subqueries
  • 3. Using a Subquery to Solve a Problem Who has a GPA higher than Owen’s? Which students have a GPA higher than Owen’s GPA? Main Query What is Owen’s GPA? Subquery ?
  • 4. Subqueries The subquery (inner query) executes once before the main query. The result of the subquery is used by the main query (outer query). SELECT select_list FROM table WHERE expr operator (SELECT select_list FROM table);
  • 5. Using a Subquery “Who has a GPA higher than Owen’s?” LastName GPA Lee 3.82 Tham 3.89 SELECT LastName, GPA from Student where GPA > (Select GPA from Student Where LastName = 'Owen'); 3.34
  • 6. Guidelines for Using Subqueries Enclose subqueries in parentheses. Place subqueries on the right side of the comparison operator. Do not add an ORDER BY clause to a subquery. Use single-row operators with single-row subqueries.
  • 7. Types of Subqueries Single-row subquery Multiple-row subquery Main query Subquery returns DIC DIC DCS Main query Subquery returns
  • 8. Single-Row Subqueries Return only one row Use single-row comparison operators Operator = > >= < <= <> Meaning Equal to Greater than Greater than or equal to Less than Less than or equal to Not equal to
  • 9. Retrieving data using Single-Row Subqueries SELECT LastName, CourseID from Student where CourseID = Select CourseID from Student Where LastName = 'Lewis'); Who study the same course as Lewis? OUTPUT: LastName CourseID Lewis DICT Nicosia DICT Maser DICT DICT
  • 10. Retrieving data using Single-Row Subqueries SELECT LastName,GroupLeader from Student where GroupLeader = (Select GroupLeader from Student Where LastName = 'Williams'); S010 Who has the same group leader as Williams? OUTPUT: LastName GroupLeader Mikulski S010 Faga S010 Williams S010
  • 11. Retrieving data using Single-Row Subqueries SELECT LastName,DateEnrolled from Student where DateEnrolled < (Select DateEnrolled from Student Where StudID = 'S009'); Who have been enrolled earlier than student S009? 01-Feb-02 OUTPUT: LastName DateEnrolled Kebel 23-Jun-01 Lee 05-Jan-02 Lewis 03-Mar-00 Law 01-Apr-01 Faga 25-Jun-01 Owen 17-Sep-01 Ng 01-Apr-01 Roche 30-Mar-00 Jann 01-Apr-01
  • 12. Retrieving data using Single-Row Subqueries with GROUP functions Display all the students that earn the minimum GPA SELECT LastName,GPA from Student where GPA = (Select min(GPA) from student); 1.88 OUTPUT: LastName GPA Ng 1.88 Roche 1.88
  • 13. Multiple-Row Subqueries Return more than one row Use the IN multiple-row comparison operator to compare an expression to any member in the list that a subquery returns
  • 14. Using Group Functions in a Multiple-Row Subquery Display all students who earn the same GPA as the minimum GPA for each course 1.88,1.89,2.22,3 SELECT LastName,GPA, CourseID from Student where GPA IN (select min(GPA) from student Group By CourseID); OUTPUT: LastName GPA CourseID Mikulski 1.89 DCS Faga 2.22 DIC Ng 1.88 DIT Maser 3 DICT Roche 1.88 DIT
  • 15. Using Group Functions in a Multiple-Row Subquery Display all students who enrolled the same course as Law and Lewis. DICT, DIT SELECT LastName, CourseID from Student where CourseID IN (select CourseID from Student where LastName IN ('Lewis','Law')) order by CourseID; OUTPUT: LastName CourseID Maser DICT Nicosia DICT Lewis DICT Roche DIT Ng DIT Law DIT Lee DIT
  • 16. Summary A subquery is a SELECT statement that is embedded in a clause of another SQL statement. Subqueries are useful when a query is based on unknown criteria. Subqueries have the following characteristics:  Can pass one row of data to a main statement that contains a single-row operator, such as =, <>, >, >=, <, or <=  Can pass multiple rows of data to a main statement that contains a multiple-row operator, such as IN  Can contain group functions