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

Chapter-9.ppt

  • 1.
  • 2.
    Objectives Describe the typesof problems that subqueries can solve Define subqueries List the types of subqueries Write single-row and multiple-row subqueries
  • 3.
    Using a Subquery toSolve 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 (innerquery) 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 “Whohas 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 UsingSubqueries 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-rowsubquery Multiple-row subquery Main query Subquery returns DIC DIC DCS Main query Subquery returns
  • 8.
    Single-Row Subqueries Return onlyone 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 usingSingle-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 usingSingle-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 usingSingle-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 usingSingle-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 morethan 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 ina 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 ina 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 isa 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