Professional Documents
Culture Documents
4 The HAVING Clause PDF
4 The HAVING Clause PDF
CPSC 421
Database Management Systems
Lecture 5:
More SQL Group By, Having,
Order By, Subqueries
Todays Agenda
Quiz
More SQL extensions
Group by
Having
Order by
Subqueries (WHERE clause)
CPSC421,2009
9/16/09
Foreignkey:Customer.SalespersonSalesperson.Number
CPSC421,2009
SQL GROUP BY
Any SQL query can have the answer grouped
In which there is one output row for each group
SELECTSalesperson,COUNT(*)
FROMCustomer;
versus
SELECTSalesperson,COUNT(*)
FROMCustomer
GROUPBYSalesperson;
CPSC421,2009
9/16/09
SQL GROUP BY
Customer
Number Name
1
smith
2
jones
3
wei
Address
xxx
yyy
zzz
CRaYng
5
7
10
CAmount
1000
5000
10000
SELECTSalesperson,COUNT(*)
FROMCustomer
GROUPBYSalesperson;
CBalance Salesperson
1000
101
4000
101
10000 NULL
Salesperson COUNT(*)
101
2
NULL
1
CPSC421,2009
SQL HAVING
Customer
Number Name
1
smith
2
jones
3
wei
Address
xxx
yyy
zzz
CRaYng
5
7
10
CAmount
1000
5000
10000
SELECTSalesperson,COUNT(*)
FROMCustomer
GROUPBYSalesperson
HAVINGCOUNT(*)>1;
CBalance Salesperson
1000
101
4000
101
10000 NULL
Salesperson COUNT(*)
101
2
9/16/09
Salesperson
5
8
NULL
5
5
2
8
SELECTSalesperson,COUNT(*)
FROMCustomer
GROUPBYSalesperson;
CPSC421,2009
Salesperson
5
8
NULL
5
5
2
8
SELECTSalesperson,COUNT(*)
FROMCustomer
GROUPBYSalesperson;
CPSC421,2009
IntermediateResult:
4Groups
103
dave
NULL
101
106
107
mary 5
sam
5
oliver 5
102
110
John
luis
109
Susan 2
8
8
9/16/09
Count(*)
1
3
2
1
QueryAnswer:
1rowpergroup!
HereweevaluateSELECTSalesperson,
Count(*)foreachgroup
SELECTSalesperson,COUNT(*)
FROMCustomer
GROUPBYSalesperson;
IntermediateResult:
4Groups
103
dave
NULL
101
106
107
mary 5
sam
5
oliver 5
102
110
John
luis
109
Susan 2
8
8
CPSC421,2009
QueryAnswer:
1rowpergroup!
HerewecheckifCOUNT(*)>1holdsforeach
group,andthenevaluateSELECTSalesperson,
Count(*)foreachgroup
SELECTSalesperson,COUNT(*)
FROMCustomer
GROUPBYSalesperson
HavingCOUNT(*)>1;
CPSC421,2009
IntermediateResult:
4Groups
103
dave
NULL
101
106
107
mary 5
sam
5
oliver 5
102
110
John
luis
109
Susan 2
8
8
10
9/16/09
CPSC421,2009
11
SQL GROUP BY
The following query is not legal (Why?)
SELECTName
FROMCustomer
GROUPBYSalesperson;
because there can be many names for each group!
CPSC421,2009
12
9/16/09
Exercise
1. Form groups of 2
2. Use this Baseball schema:
Team(Name,Games,Wins,Losses,Conference)
Player(Name,Hits,AtBats,HomeRuns,Team)
Player.Team>Team.Name
13
SQL ORDER BY
Used to sort the result of a query
SELECTNumber,Name,Salesperson
FROMCustomer
ORDERBYName;
Number Name
101
mary
102
john
103
dave
106
sam
107
oliver
109
susan
110
luis
111
mary
Salesperson
5
8
NULL
5
5
2
8
4
Number Name
103
dave
102
john
110
luis
101
mary
111
mary
107
oliver
106
sam
109
susan
CPSC421,2009
Salesperson
NULL
8
8
5
4
5
5
2
14
9/16/09
SQL ORDER BY
Used to sort the result of a query
SELECTNumber,Name,Salesperson
FROMCustomer
ORDERBYNameDESC;
Number Name
101
mary
102
john
103
dave
106
sam
107
oliver
109
susan
110
luis
111
mary
Salesperson
5
8
NULL
5
5
2
8
4
Number Name
109
susan
106
sam
107
oliver
111
mary
101
mary
110
luis
102
john
103
dave
CPSC421,2009
Salesperson
2
5
5
4
5
8
8
NULL
15
SQL ORDER BY
Used to sort the result of a query
SELECTNumber,Name,Salesperson
FROMCustomer
ORDERBYName,Salesperson;
Number Name
101
mary
102
john
103
dave
106
sam
107
oliver
109
susan
110
luis
111
mary
Salesperson
5
8
NULL
5
5
2
8
4
Number Name
103
dave
102
john
110
luis
111
mary
101
mary
107
oliver
106
sam
109
susan
CPSC421,2009
Salesperson
NULL
8
8
4
5
5
5
2
16
9/16/09
SQL Subqueries
Subqueries can be used in the where clause (in addition
to the from clause)
SELECTC1.Number,C1.Name
FROMCustomerC1
WHEREC1.CRaYng=(SELECTMAX(C2.CraYng)
FROMCustomerC2);
The subquery is the Inner Query and the rest is the
Outer Query
CPSC421,2009
17
SQL Subqueries
What does this query return?
SELECTC1.Number,C1.Name
FROMCustomerC1
WHEREC1.CRaYng=(SELECTMAX(C2.CRaYng)
FROMCustomerC2);
What does the inner query return?
A single value (the max credit rating)
18
9/16/09
SQL Subqueries
How would you evaluate this query?
SELECTC1.Number,C1.Name
FROMCustomerC1
WHEREC1.CRaYng=(SELECTMAX(C2.CRaYng)
FROMCustomerC2);
Start with the FROM clause in the outer query
Take a row from the Customer table
Check if the row satisfies the WHERE clause
which involves evaluating the inner query
CPSC421,2009
19
SQL Subqueries
Customer
Number Name
1
Smith
2
Jones
3
Wei
Address
123XSt
222YSt
111ZSt
CRaYng
5
7
10
CAmount
10000
8000
11000
CBalance Salesperson
9000
55
3750
65
9000
NULL
SELECTC1.Number,C1.Name
FROMCustomerC1
WHEREC1.CRaYng=(SELECTMAX(C2.CRaYng)
FROMCustomerC2);
Here, we can evaluate the inner query first (answer: 10)
For each row, is CRating equal to the inner query answer?
CPSC421,2009
20
10
9/16/09
SQL Subqueries
Subqueries can be used in the where clause (in addition
to the from clause)
SELECTC1.Number,C1.Name
FROMCustomerC1
WHEREC1.CRaYng=(SELECTMAX(C2.CraYng)
FROMCustomerC2);
The comparator can be any of the six standard ones:
=, >, < >=, <=, <> (not equal)
For these, the inner query must return a single value!
CPSC421,2009
21
SQL Subqueries
Subqueries can be used in the where clause (in addition to
the from clause)
SELECTC1.Number,C1.Name
FROMCustomerC1
WHEREC1.CRaYng=(SELECTMAX(C2.CraYng)
FROMCustomerC2);
If the inner query does not mention any attributes from
the outer query (C1 not mentioned in the inner query)
Then you only need to evaluate the inner query once
The inner (sub) query is NOT correlated
CPSC421,2009
22
11
9/16/09
23
CPSC421,2009
24
12
9/16/09
CPSC421,2009
25
CPSC421,2009
26
13
9/16/09
CPSC421,2009
27
28
14
9/16/09
29
30
15
9/16/09
31
For Thursday
Reading
Make sure you are caught up in the reading
Well finish up SQL queries on Thursday and start on
conceptual modeling
Be sure to know:
Group by, having, order by, subqueries
Homework
Homework 2 due on Thursday
Should form groups this week, start on project proposal
CPSC421,2009
32
16