SlideShare une entreprise Scribd logo
1  sur  16
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

Contenu connexe

Similaire à 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
 

Dernier

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Dernier (20)

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 

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