2 - SQL - Part 4
2 - SQL - Part 4
Part 4
1
Aggregation Functions
2
Aggregation
• In addition to retrieving data, we can use SQL to summarize data or perform some
computation
• SQL supports operations for computing aggregate values of any column
select aggregation(column)
from table
3
Aggregation Functions
select aggregation(column)
from table
4
Aggregation - COUNT
Sailors (sid,name,rating,age)
sid name rating age
Find the number of sailors
1 Dusty 7 45
Output 2 Rusty 10 35
select count(*) count 3 Horatio 5 35
from Sailors; 5 4 Horatio 10 18
5 Julius null 25
Sailors (sid,name,rating,age)
sid name rating age
Find the minimum rating
1 Dusty 7 45
Output 2 Rusty 10 35
select min(rating) min 3 Horatio 5 35
from Sailors; 5 4 Horatio 10 18
5 Julius null 25
Sailors (sid,name,rating,age)
sid name rating age
Find the average rating of sailors
1 Dusty 7 45
Output 2 Rusty 10 35
select avg(rating) avg 3 Horatio 5 35
from Sailors; 8.00 4 Horatio 10 18
5 Julius null 25
Sailors (sid,name,rating,age)
sid name rating age
1 Dusty 7 45
2 Rusty 10 35
3 Horatio 5 35
4 Horatio 10 18
5 Julius null 25
Find the number of sailors, their minimum age and average rating
Sailors (sid,name,rating,age)
sid name rating age
1 Dusty 7 45
2 Rusty 10 35
3 Horatio 5 35
4 Horatio 10 18
5 Julius null 25
Find the name and age of the oldest This is illegal in SQL;
name must be used in an
sailor
aggregate function or must
select name, max(age) appear in the group by clause
from Sailors; (Although some tools allow this,
e.g., sqlite)
Sailors (sid,name,rating,age)
What does this query compute? sid name rating age
1 Dusty 7 45
2 Rusty 10 35
select count(*)
3 Horatio 5 35
from (select rating from sailors); 4 Horatio 10 18
5 Julius null 25
Answer:
count
5
WARNING !
Find the names of sailors who are older than the oldest sailor with a raiting of 10
Sailors (sid,name,rating,age)
select [Link] sid name rating age
from Sailors S 1 Dusty 7 45
where [Link] > (select max([Link]) name
from Sailors S2 2 Rusty 10 35
Dusty
where [Link]=10); 3 Horatio 5 35
4 Horatio 10 18
5 Julius null 25
Can also be written as
select [Link]
from Sailors S
where [Link] > all (select [Link] Not Preferred:
from Sailors S2
where [Link]=10); ALL query is more
error prone – one
could easily (and
incorrectly) use
ANY instead of ALL
Question
Sailors (sid,name,rating,age)
sid name rating age
select rating, min(age)
1 Dusty 7 45
from Sailors
2 Rusty 10 35
where age > 35
3 Horatio 5 35
group by rating;
4 Horatio 10 18
5 Julius null 25
A B C
rating min rating min rating min
7 45 7 45 7 45
10 null 10 null
5 null 5 null
null null
C
12
aggregate_function_name( [ALL | DISTINCT] expression )
Function Usage
AVG(expression) Computes the average value of a column given by
expression
CORR(dependent, independent) Computes a correlation coefficient
Function Usage
MIN(expression) Finds the minimum value in a column given by
expression
MAX(expression) Finds the maximum value in a column given by
expression
PERCENT_RANK(value_list) WITHIN Generates a relative rank for a hypothetical row by
GROUP (ORDER BY sort_list) dividing that row’s rank less 1 by the number of
rows in the group
PERCENTILE_CONT(percentile) WITHIN Generates an interpolated value that, if added to
GROUP (ORDER BY sort_list) the group, would correspond to the percentile
given
PERCENTILE_DISC(percentile) WITHIN Returns the value with the smallest cumulative
GROUP (ORDER BY sort_list) distribution value greater than or equal to
percentile
RANK(value_list) WITHIN GROUP (ORDER Generates a rank for a hypothetical row
BY sort_list) (value_list) in a group of rows generated by
GROUP BY
aggregate_function_name( [ALL | DISTINCT] expression )
Function Usage
Function Usage
17
Aggregate Functions vs Group-by and Having Clauses
18
The need for Group-by
Find the age of the youngest sailor for each rating level
Sailors (sid,name,rating,age)
select min(age) select min(age) select min(age) sid name rating age
from Sailors from Sailors from Sailors
where rating = 1; where rating = 2; where rating = 3;
1 Dusty 7 45
2 Rusty 10 35
3 Horatio 5 35
… 4
5
Horatio
Julius
10
null
18
25
Find the age of the youngest sailor for each rating level
Sailors (sid,name,rating,age)
select rating, min(age) sid name rating age
from Sailors 1 Dusty 7 45
group by rating; 2 Rusty 10 35
3 Horatio 5 35
4 Horatio 10 18
5 Julius null 25
Answer:
rating min
7 45
A single value 10 18
per group 5 35
null 25
Group-by and Having Clauses
Find the age of the youngest sailor for each rating level
Sailors (sid,name,rating,age)
Answer: sid name rating age
select rating, min(age) rating min 1 Dusty 7 45
from Sailors 7 45 2 Rusty 10 35
group by rating; 10 18 3 Horatio 5 35
5 35 4 Horatio 10 18
null 25 5 Julius null 25
Find the age of the youngest sailor for each rating level over 35
Answer:
rating min
select rating, min(age)
7 45
from Sailors
where rating > 5 10 18
group by rating;
Group-by and Having Clauses
Find the age of the youngest sailor for each rating level
Sailors (sid,name,rating,age)
sid name rating age
select rating, min(age) 1 Dusty 7 45
from Sailors 2 Rusty 10 35
group by rating; 3 Horatio 5 35
4 Horatio 10 18
5 Julius null 25
Find the age of the youngest sailor for each rating level, such that there are at least two sailors
with that rating
Sailors (sid,name,rating,age)
sid name rating age
22 Dusty 7 45
29 Brutus 1 33
31 Lubber 8 55.5
32 Andy 8 25.5
58 Rusty 10 35.0
64 Horatio 7 35
71 Zorba 10 16
74 Horatio 9 35
85 Art 3 25.5
95 Bob 3 63.5
96 Frodo 3 25.5
Aggregation – Evaluation Steps
85 Art 3 25.5
95 Bob 3 63.5
96 Frodo 3 25.5
Step 1 –
• We compute the cartesian product of all tables in the from clause
• Here we have only one table, so we move on to the next step
Evaluation – Step 2
96 Frodo 3 25.5 7 45
1 33
8 55.5
8 25.5
10 35.0
Step 3 – 7 35
• We eliminate unwanted columns – we keep 9 35
only columns in the select clause, the group-by 3 25.5
clause and the having clause 3 63.5
• We eliminate the columns sid and name 3 25.5
Evaluation – Step 4
Step 4 – 8 55.5
8
• We sort the table in groups according to the 25.5
9 35
columns in the group by clause
10 35
• We sort the table by rating
Evaluation – Step 5
rating age
3 25.5
7 35
8 25.5
Step 6 –
• We apply the select clause and generate one answer row for each
group
• We find the min(age) for each rating
Having Clause
Rule 1: a query that uses aggregate operators must use only aggregate
operators in the select clause, unless the query contains a group by clause
Rule 2: all non-aggregate columns in the select clause must appear in the
group by clause
select min(age)
from Sailors
group by rating Not Valid
having name =‘Horatio’;
select min(age)
from Sailors
group by rating, name
Valid
having name = ‘Horatio’;
Group-by and Having Clauses Examples
Find the number of reservations for each boat that was reserved at least 3 times.
Find the number of reservations for each boat that was reserved at least 3 times.
Find the number of boat reservations by each sailor who is at least 20 years old.
The question is
1) Do we have groups? (group by)
Reserves (sid, bid,day) 2) Do we quality the groups or the records?
sid bid day 3) Do we quality the records before the
1 101 10/10/12 groups are created (where) or after
1 102 10/10/12 (select) ?
1 101 10/7/12
2 102 11/9/12
2 102 7/11/12
3 101 7/11/12
Sailors (sid,name,rating,age)
3 102 7/8/12 sid name rating age
1 Dusty 7 45
4 103 19/9/12
2 Rusty 10 35
3 Horatio 5 35
4 Zorba 8 18
5 Julius 25
Group-by and Having Clauses Examples
Find the number of boat reservations by each sailor who is at least 20 years old.
Find the number of boat reservations by each sailor who has made at least 2
reservations.
The question is
1) Do we have groups? (group by)
Reserves (sid, bid,day) 2) Do we quality the groups or the records?
sid bid day 3) Do we quality the records before the
1 101 10/10/12 groups are created (where) or after
1 102 10/10/12 (select) ?
1 101 10/7/12
2 102 11/9/12
2 102 7/11/12
3 101 7/11/12
Sailors (sid,name,rating,age)
3 102 7/8/12 sid name rating age
1 Dusty 7 45
4 103 19/9/12
2 Rusty 10 35
3 Horatio 5 35
4 Zorba 8 18
5 Julius 25
Group-by and Having Clauses Examples
Find the number of boat reservations by each sailor who has made at least 2
reservations.
Find the number of reservations for each boat that was reserved at least 3 times.
The question is
Reserves (sid, bid,day) 1) Do we have groups? (group by)
sid bid day
2) Do we quality the groups or the records?
3) Do we quality the records before the
1 101 10/10/12
groups are created (where) or after
1 102 10/10/12
(select) ?
1 101 10/7/12
2 102 11/9/12
2 102 7/11/12
3 101 7/11/12
Sailors (sid,name,rating,age)
3 102 7/8/12 sid name rating age
1 Dusty 7 45
4 103 19/9/12
2 Rusty 10 35
3 Horatio 5 35
4 Zorba 8 18
5 Julius 25
Group-by and Having Clauses Examples
Find the number of reservations for each boat that was reserved at least 3 times.
Retrieve all information about reservations, sorted by bid (descending), with ties
broken by sid (ascending)
select *
from Reserves
order by bid desc, sid;
Retrieve bids of all reserved boats, along with the number of times they were
reserved, and the average sailor rating, sorted by average sailor rating
45
Null Values – Comparisons using null
• When we compare a value with null using <, > =, the result is null
• When we compare two null values using <, >, =, the result in also null
• When the result is null, the expression evaluates to false
A B Not A A or B A and B A = B
F F T F F T
F T T T F F
F null T null null null
3-way logic
T F F T F F
T T F T T T
T null F null null null
null F null null null null
null T null null null null
null null null null null null
SQL operators and null
49
Running Example - Join
select columns
from A, B
where join condition
Some vendors
support only this
form
Alternate syntax
select columns
from A join B on join condition
Different Types of Joins
Join
from A join B
Left Join
from A left join B
Right Join
from A right join B
Full Join
from A full join B
select columns
from A left outer join B
where join condition
Alternate syntax
select columns
from A left outer join B on join condition
Some vendors
support only this
form
Running Example – Left Outer Join
Answer: Yes
Outer Join
1999 SQL:1999 Added regular expression matching, recursive queries (e.g. transitive closure),
(aka SQL 3) triggers, support for procedural and control-of-flow statements, non-scalar types
(arrays), and some object-oriented features (e.g. structured types). Support for
embedding SQL in Java (SQL/OLB) and vice versa (SQL/JRT).
2003 SQL:2003 Introduced XML-related features (SQL/XML), window functions, standardized
sequences, and columns with auto-generated values (including identity-columns).
2006 SQL:2006 ISO/IEC 9075-14:2006 defines ways that SQL can be used with XML. It defines ways
of importing and storing XML data in an SQL database, manipulating it within the
database, and publishing both XML and conventional SQL-data in XML form. In
addition, it lets applications integrate queries into their SQL code with XQuery, the
XML Query Language published by the World Wide Web Consortium (W3C), to
concurrently access ordinary SQL-data and XML documents.[34]
2008 SQL:2008 Legalizes ORDER BY outside cursor definitions. Adds INSTEAD OF triggers, TRUNCATE
statement,[35] FETCH clause.
2011 SQL:2011 Adds temporal data (PERIOD FOR)[36] (more information at: Temporal
database#History). Enhancements for window functions and FETCH clause.[37]
2016 SQL:2016 Adds row pattern matching, polymorphic table functions, JSON.
2019 SQL:2019 Adds Part 15, multidimensional arrays (MDarray type and operators).
SQL Standard and Vendor Compatibility
61
Acknowledgements
62