Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 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 Owens?

Main Query

Which students have a GPA higher than


Owens GPA?
Subquery

What is Owens GPA?

Subqueries
SELECT
FROM
WHERE

The

select_list
table
expr operator
(SELECT
FROM

select_list
table);

subquery (inner query) executes once before the main query.


The result of the subquery is used by the main query (outer query).

Using a Subquery
Who has a GPA higher than Owens?
SELECT LastName, GPA
from Student
3.34
where GPA >
(Select GPA from Student
Where LastName = 'Owen');

LastName

GPA

Lee

3.82

Tham

3.89

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
Main query
Subquery

returns

DIC

Multiple-row subquery
Main query
Subquery

returns

DIC
DCS

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


Who study the same course as Lewis?
SELECT LastName, CourseID
DICT
from Student
where CourseID =
(Select CourseID from Student
Where LastName = 'Lewis');
OUTPUT:
LastName

CourseID

Lewis

DICT

Nicosia

DICT

Maser

DICT

Retrieving data using Single-Row Subqueries


Who has the same group leader as Williams?
S010
SELECT LastName,GroupLeader
from Student
where GroupLeader = (Select GroupLeader from Student
Where LastName = 'Williams');
OUTPUT:
LastName

GroupLeader

Mikulski

S010

Faga

S010

Williams

S010

Retrieving data using Single-Row Subqueries


Who have been enrolled earlier than student S009?
SELECT LastName,DateEnrolled
01-Feb-02
from Student
where DateEnrolled < (Select DateEnrolled from Student
Where StudID = 'S009');
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);
OUTPUT:
LastName

GPA

Ng

1.88

Roche

1.88

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
SELECT LastName,GPA, CourseID
1.88,1.89,2.22,3
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

Roche

1.88

DICT
DIT

Using Group Functions


in a Multiple-Row Subquery

Display all students who enrolled the same course as Law and Lewis.

SELECT LastName, CourseID


DICT, DIT
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

You might also like