0% found this document useful (0 votes)
56 views

Yet More SQL Select: Database Systems Lecture 9

The document summarizes SQL aggregate functions and how to use GROUP BY and HAVING clauses. It discusses: 1. Common aggregate functions like COUNT, SUM, AVG, MIN, MAX and how to use them in SELECT statements. 2. How GROUP BY allows organizing results by one or more columns and computing aggregates for each group. 3. How HAVING works similarly to WHERE but applies conditions to groups after aggregation rather than rows. 4. How UNION can combine results from multiple SELECT statements if they have matching columns.

Uploaded by

SowlSnatcha
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Yet More SQL Select: Database Systems Lecture 9

The document summarizes SQL aggregate functions and how to use GROUP BY and HAVING clauses. It discusses: 1. Common aggregate functions like COUNT, SUM, AVG, MIN, MAX and how to use them in SELECT statements. 2. How GROUP BY allows organizing results by one or more columns and computing aggregates for each group. 3. How HAVING works similarly to WHERE but applies conditions to groups after aggregation rather than rows. 4. How UNION can combine results from multiple SELECT statements if they have matching columns.

Uploaded by

SowlSnatcha
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Yet More SQL SELECT

Database Systems Lecture 9


Last Lecture

2. Aliases & Self-joins


3. Subqueries
• IN, EXISTS, ANY, ALL

• For more information


• Connoly and Begg Chapter 5
Here we go again:
• Yet more SQL
• Aggregate functions
• GROUP BY and HAVING
• UNION, etc.

• For more information


• Connoly and Begg Chapter 5
Aggregate Functions
• First, it is important to know that Mark/100
SELECT as well as
column names, you can select constants,
FROM Grades
compute arithmetic expressions and evaluate
functions in a SELECT statement
SELECT
Salary + Bonus
FROM Employee

SELECT 1.175*Price
FROM Products
Aggregate Functions
• Aggregate functions compute summaries of
data in a The
• COUNT: table
number of rows
• Most The
SUM: sum offunctions
aggregate the entries
(allinexcept
a column
COUNT) work on
• a single
AVG: Thecolumn ofentry
average numeric
in adata
column
• Use
MIN, an alias
MAX: tominimum
The name theandresult
maximum entries in a
column
Aggregate Functions
Grades SELECT Count
Name Code Mark COUNT(*) AS Count
FROM Grades 6
Pingu DBS56
Pingu IAI 72
Zippy DBS60
Bungle PR1 43 SELECT Total
Bungle PR2 35 SUM(Mark) AS Total
Pikachu IAI 54 FROM Grades 320

SELECT Best
MAX(Mark) AS Best
FROM Grades 72
Aggregate Functions
• You can combine SELECT
aggregate functions MAX(Mark)-MIN(Mark)
using arithmetic
AS Range
Grades FROM Grades
Name Code Mark
Pingu DBS56
Pingu IAI 72
Zippy DBS60 MAX(Mark) = 72 Range
Bungle PR1 43
Bungle PR2 35 37
Pikachu IAI 54 MIN(Mark) = 35
Join Example
Modules
• Find Bungle’s average mark, weighted by the
credits of each Credits
Code Title module
DBS Database Sys. 10
GRP Group Project 20
PRG Programming 10

Grades SELECT AVE(Mark*Credits)


FROM Modules, Grades
Name Code Mark
WHERE Modules.Code=Grades.Code
Zippy DBS 60 AND Grades.Name = ‘Bungle’
Bungle GRP 47
Pingu PRG 56
GROUP BY
• Every entry in <cols1> must be in <cols2>,
be a constant, or be an aggregate function
SELECT <cols1>
You<tables>
•FROM can have WHERE or ORDER BY clauses as
well as
GROUP BY a<cols2>
GROUP BY clause
GROUP BY
• Find the total value of the sales for each
Sales
department
Month in each month
Department Value
• Can group by Month then Department or
March Fiction 20
Department then Month
March Travel 30
• Same results, but in a different order
March Technical 40
April Fiction 10
April Fiction 30
April Travel 25
April Fiction 20
May Fiction 20
May Technical 50
GROUP BY
SELECT Month, Department, SELECT Month, Department,
SUM(Value) AS Total SUM(Value) AS Total
FROM Sales FROM Sales
GROUP BY Month, Department GROUP BY Department, Month
Month Department Total Month Department Total
AprilFiction 60 AprilFiction 60
AprilTravel 25 March Fiction 20
March Fiction 20 May Fiction 20
March Technical 40 March Technical 40
March Travel 30 May Technical 50
May Fiction 20 AprilTravel 25
May Technical 50 March Travel 30
GROUP BY
• Sometimes
The GROUP BYweclause
want to
does
apply
thisaggregate
functions to groups of rows
SELECT <cols1>
Example,
•FROM find the average mark of each
<tables>
student
GROUP BY <cols2>
GROUP BY
SELECT Name,
AVG(Mark) AS Average
Grades FROM Grades
Name Code Mark GROUP BY Name
Pingu DBS56
Pingu IAI 72
Zippy DBS60 Name Average
Bungle PR1 43
Bungle PR2 35 Pingu 64
Pikachu IAI 54 Zippy 60
Bungle 39
Pikachu 54
HAVING
• HAVING is like a WHERE SELECT Name,
clause, except that it
applies to the results of AVG(Mark)
a GROUP BYAS Average
query
FROM Grades
• It can be used to select GROUP BY Name
groups which satisfy a
HAVING AVG(Mark) >= 40
given condition
Name Average
Pingu 64
Zippy 60
Pikachu 54
WHERE and HAVING
• WHERE refers to the rows• of
Think
tables,
of aand
query
so
cannot use aggregate functions
being processed as
follows:
• HAVING refers to the groups of rows, and so
cannot use columns which are not
• Tables are in the
combined
GROUP BY • WHERE clauses
• GROUP BY and Aggregates
• Column selection
• HAVING clauses
• ORDER BY
UNION, etc.
•• UNION, INTERSECT, and
They all combine the results from two select statements
EXCEPT
• The resultstreat
• Explicitly of the
thetwo selects must have the same
tables
columns andare
as sets and data
the types
usual set operators of
union, intersection, and
difference.

• We’ll concentrate on
UNION.

• Oracle has MINUS


instead of EXCEPT.
UNION
• Find, in a single query the average mark for
Grades
each
Name student,
Code and the average mark overall
Mark
Pingu DBS56
Pingu IAI 72
Zippy DBS60
Bungle PR1 43
Bungle PR2 35
Pikachu IAI 54
UNION
• The average for each • The average overall
student:
SELECT ‘Total’ AS Name,
SELECT Name, AVG(Mark) AS Average
AVG(Mark) AS Average FROM Grades
FROM Grades
• Note - this has the
GROP BY Name
same columns as the
average by student
UNION
SELECT Name
AVG(Mark) AS Average
FROM Grades
GROP BY Name Name Average
Pikachu 52
UNION Pingu 64
Bungle 39
SELECT Zippy 60
'Total' as Name, Total 53
AVG(Mark) AS Average
FROM Grades
A Final Example
• We
Examiners’
want thereports
results
• Sorted
We wantbyayear
list ofthen
students
average
and
mark
their(High
average
to low)
mark
• then lastand
For first name, first years
second name,the
and finally ID
average is for that
• year
To take into account the number of credits each
• module is worth
For finalists it is 40% of the second year plus 60% of
• the final year
Produced by aaverage.
single query
Tables for the Example
Student
ID First Last Year

Grade
ID Code Mark YearTaken

Module
Code Title Credits
We’ll Need a UNION
• Finalists are treated differently
• Write one query for the finalists
• Write a second query for the first and
<QUERY FORsecond years
FINALISTS>
• Use a UNION to join them together
UNION

<QUERY FOR OTHERS>


We’ll need to Join the
Tables
• Both
This is
ofathe
natural
subqueries
join operation
need information from
all thecould
• We tables
use a NATURAL JOIN statement, and hope
• that our version
The student of SQLand
ID, name canyear
do it
•• Safer to just
The marks foruse
eacha WHERE
moduleclause
and the year taken
• The number of credits for each module
The Query So Far
SELECT <some information>
FROM Student, Module, Grade
WHERE Student.ID = Grade.ID
AND Module.Code = Grade.Code
AND <student is in third year>
UNION
SELECT <some information>
FROM Student, Module, Grade
WHERE Student.ID = Grade.ID
AND Module.Code = Grade.Code
AND <student is in first or second year>
Information for Finalists
The
•We average
need is hard
to retrieve
•• We don’t have
Compute anymark,
average statement to separate
weighted years 2
40-60 across
and
years32easily
and 3
• We can exploit the fact that 40 = 20*2 and 60 =
• First year marks need to be ignored
20*3, so YearTaken and the weighting have a simple
• The ID, Name, and Year are needed as they are used
relationship
for ordering
Information for Finalists

SELECT Year, Student.ID, Last, First,


SUM((20*YearTaken/100)*Mark*Credits)/120
AS AverageMark
FROM Student, Module, Grade
WHERE Student.ID = Grade.ID
AND Module.Code = Grade.Code
AND YearTaken IN (2,3)
AND Year = 3
GROUP BY Year, Student.ID, First, Last
Information for Other
Students
• Other students are easier than finalists
• We just need to average their marks where
YearTaken and Year are the same
• As before we need the ID, Name, and Year
for ordering
Information for Other
Students
SELECT Year, Student.ID, Last, First,
SUM(Mark*Credits)/120 AS AverageMark
FROM Student, Module, Grade
WHERE Student.ID = Grade.ID
AND Module.Code = Grade.Code
AND YearTaken = Year
AND Year IN (1,2)
GROUP BY Year, Student.ID, First, Last
The Final Query
SELECT Year, Student.ID, Last, First,
SUM((20*YearTaken/100)*Mark*Credits)/120 AS AverageMark
FROM Student, Module, Grade
WHERE Student.ID = Grade.ID AND Module.Code = Grade.Code
AND YearTaken IN (2,3) AND Year = 3
GROUP BY Year, Student.ID, First, Last

UNION

SELECT Year, Student.ID, Last, First,


SUM(Mark*Credits)/120 AS AverageMark
FROM Student, Module, Grade
WHERE Student.ID = Grade.ID AND Module.Code = Grade.Code
AND YearTaken = Year AND Year IN (1,2)
GROUP BY Year, Student.ID, First, Last

ORDER BY Year desc, AverageMark desc, First, Last, ID


Next Lecture
• SQL at the Coal Face!
• Limitations of SQL
• SQL from within other Languages
• SQL, Java, JDBC and PHP

• For More Information


• Connolly and Begg 29.7

Yet More SQL SELECT

You might also like