T3 L2 Groupby
T3 L2 Groupby
Topic 3
Lesson 2 – SQL SELECT Aggregation Queries
Chapter 6 Murach’s MySQL
2
Aggregate functions in MySQL
AVG([ALL|DISTINCT] expression)
SUM([ALL|DISTINCT] expression)
MIN([ALL|DISTINCT] expression)
MAX([ALL|DISTINCT] expression)
COUNT([ALL|DISTINCT] expression)
COUNT(*)
GROUP_CONCAT([ALL|DISTINCT] expression)
3
Aggregate function keywords in SQL
All aggregate functions by default exclude NULL values before
working on the data.
The DISTINCT keyword omits duplicates from the results.
The ALL keyword includes even duplicates. If nothing is
specified, the ALL is the default behavior.
4
AVG and SUM in MySQL
AVG([ALL|DISTINCT] expression)
SUM([ALL|DISTINCT] expression)
5
MIN and MAX functions in MySQL
MIN([ALL|DISTINCT] expression)
MAX([ALL|DISTINCT] expression)
6
COUNT function in MySQL
COUNT([ALL|DISTINCT] expression)
COUNT(*)
7
GROUP_CONCAT function in MySQL
GROUP_CONCAT([ALL|DISTINCT] expression)
8
Examples: Aggregate functions in MySQL
SELECT COUNT(*) FROM student;
SELECT COUNT(*) AS num_students FROM student;
SELECT SUM(credits_earned) Khoury_Earned FROM
student WHERE school = ‘Khoury’;
SELECT COUNT(DISTINCT school) AS num_schools FROM
student;
9
Solution: Aggregate functions in MySQL
SELECT COUNT(*) FROM students;
SELECT COUNT(*)AS num_students FROM students;
SELECT SUM(Credits_Earned) Khoury_Earned FROM
students WHERE School = ‘Khoury’;
SELECT COUNT(DISTINCT School) AS num_schools FROM
students;
10
Examples 2: Aggregate functions in MySQL
SELECT MIN(credits_req) FROM students;
SELECT MAX(credits_earned)AS max_credits FROM
students;
SELECT MAX(credits_earned)AS max_credits FROM
students WHERE school = ‘Khoury’;
11
Solution: Aggregate functions in MySQL
SELECT MIN(credits_req) FROM students;
SELECT MAX(credits_earned) AS max_credits FROM
students;
SELECT MAX(credits_earned)AS khoury_earned FROM
students WHERE school = ‘Khoury’;
12
GROUP BY clause
Sometimes you want to stratify the qualifying tuples by a
specific field in the table. Use the GROUP BY clause to
define groups of tuples. It produces a result tuple for each
combination of the fields’ values in the GROUP BY list. The
GROUP BY clause has a supporting HAVING clause. The
syntax for the SELECT statement with the GROUP BY
clause is:
SELECT select_list
FROM table_source
[WHERE search_condition]
[GROUP BY group_by_list]
[HAVING search_condition]
[ORDER BY order_by_list]
13
Examples: GROUP BY clause
SELECT school FROM students GROUP BY school;
SELECT school, credits_required FROM students
GROUP BY school, credits_required;
14
Solution: GROUP BY clause
SELECT school FROM students GROUP BY school;
SELECT school, credits_required FROM students
GROUP BY school, credits_required;
15
Combining Aggregation and GROUP BY
School Max(credits_earned)
Khoury 50
D’Amore McKim 64
16
Solution query: max credit by school
Student
School Max(credits_earned)
Khoury 50
D’Amore McKim 64
17
HAVING clause creates a filter
18
Example using the HAVING clause
Student
19
Solution
GROUP BY result
School Max(credits_earned)
Khoury 50
D’Amore McKim 64
School Max(credits_earned)
Having result
D’Amore McKim 64
20
WITH ROLLUP
An extension to the GROUP BY clause, it includes summary
rows in the result for each subgroup in your groups. It
generates grand totals and subtotals for reports.
It has not been accepted into the SQL Standard but most
vendors have implemented it.
21
Solution
GROUP BY result
School SUM(credits_earned) School SUM(credits_earned)
Khoury 82 Khoury 82
146
Rollup result
22
Summary
In this module you learned:
• Aggregation functions can be used in the field list
as well as in the HAVING clause
• SQL SELECT clauses: GROUP BY and HAVING
clause
• Necessary coordination between the field list and
the GROUP BY list
SQL 23
MySQL work
Let’s use the MySQL workbench to write Aggregation
statements using the ap database
24
SQL work 2
Return the vendor name and vendor contact name as one
field
Return the vendor names that start with P
Count the number of vendors
Count the number of invoices per vendor
Calculate the total payment by vendor, return the vendor
name and the total
Calculate the total payment by vendor, return the vendor id
and the total payment for vendor for vendors who paid more
than $100
25