Yet More SQL Select: Database Systems Lecture 9
Yet More SQL Select: Database Systems Lecture 9
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
• We’ll concentrate on
UNION.
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
UNION