Aggregation: GROUP BY and HAVING · Lesson 10 of 30
SQL Aggregate Functions: COUNT, SUM, AVG
Managers don't ask for 10,000 order rows – they ask "how many orders did we get?", "what's our revenue?", "what's the average order value?". Aggregate functions answer these by collapsing many rows into one summary value. They're the foundation of every report, dashboard and analytics query.
In this lesson
COUNT,SUM,AVG,MIN,MAXCOUNT(*)vsCOUNT(column)vsCOUNT(DISTINCT column)- How NULLs affect aggregates
GROUP_CONCATfor lists
The five core aggregates
SELECT COUNT(*) AS employees,
SUM(salary) AS payroll,
ROUND(AVG(salary)) AS average_salary,
MIN(salary) AS lowest,
MAX(salary) AS highest
FROM employees;
+-----------+---------+----------------+--------+---------+ | employees | payroll | average_salary | lowest | highest | +-----------+---------+----------------+--------+---------+ | 10 | 1223000 | 122300 | 65000 | 250000 | +-----------+---------+----------------+--------+---------+
What an aggregate does
- FROM + WHEREpick the rows (here: all 10 employees)
- Aggregatecombine the values of those rows into one
- Resulta single summary row
Aggregates with WHERE
WHERE filters rows before they're aggregated:
SELECT COUNT(*) AS items_sold_lines,
SUM(quantity) AS units,
SUM(quantity * unit_price) AS revenue
FROM order_items
WHERE order_id IN (SELECT order_id FROM orders WHERE status = 'delivered');
+------------------+-------+----------+ | items_sold_lines | units | revenue | +------------------+-------+----------+ | 11 | 14 | 10834.00 | +------------------+-------+----------+
(The inner SELECT is a subquery – chapter 6 covers them; here it simply lists the delivered order IDs.)
COUNT(*) vs COUNT(column) vs COUNT(DISTINCT)
SELECT COUNT(*) AS all_rows,
COUNT(city) AS rows_with_city,
COUNT(DISTINCT city) AS different_cities
FROM customers;
+----------+----------------+------------------+ | all_rows | rows_with_city | different_cities | +----------+----------------+------------------+ | 8 | 7 | 5 | +----------+----------------+------------------+
| Form | Counts |
|---|---|
COUNT(*) | every row, NULLs or not |
COUNT(col) | rows where col is not NULL (Neha's NULL city is skipped) |
COUNT(DISTINCT col) | different non-NULL values (Pune and Delhi appear twice but count once) |
-- How many customers have actually placed an order?
SELECT COUNT(*) AS orders, COUNT(DISTINCT customer_id) AS ordering_customers
FROM orders;
+--------+--------------------+ | orders | ordering_customers | +--------+--------------------+ | 12 | 7 | +--------+--------------------+
NULLs are ignored – mostly
All aggregates except COUNT(*) skip NULL values. That's usually what you want, but it can quietly change averages:
SELECT COUNT(*) AS products,
COUNT(category_id) AS with_category,
AVG(category_id IS NULL) AS share_without_category,
SUM(stock) AS total_stock
FROM products;
+----------+---------------+------------------------+-------------+ | products | with_category | share_without_category | total_stock | +----------+---------------+------------------------+-------------+ | 10 | 9 | 0.1000 | 1175 | +----------+---------------+------------------------+-------------+
AVG() of a true/false test gives a share: 0.1000 means 10% of products have no category. That trick works because MySQL treats true as 1 and false as 0.
Averages and NULL
If three students scored 80, 60 and NULL (absent), AVG(score) is 70 – the absent student is ignored, not counted as 0. If absent should mean 0, write AVG(COALESCE(score, 0)), which gives 46.67. Decide which one the business actually means.
SELECT SUM(salary) AS total FROM employees WHERE department = 'Finance';
+-------+ | total | +-------+ | NULL | +-------+
With no matching rows, SUM, AVG, MIN and MAX return NULL (not 0), while COUNT returns 0. Use COALESCE(SUM(salary), 0) when a report must show 0.
MIN and MAX on text and dates
SELECT MIN(order_date) AS first_order, MAX(order_date) AS latest_order FROM orders;
SELECT MIN(name) AS first_alphabetically, MAX(signup_date) AS newest_signup FROM customers;
+-------------+--------------+ | first_order | latest_order | +-------------+--------------+ | 2025-08-02 | 2025-11-06 | +-------------+--------------+ +----------------------+---------------+ | first_alphabetically | newest_signup | +----------------------+---------------+ | Arjun Singh | 2025-07-30 | +----------------------+---------------+
GROUP_CONCAT: aggregate into a list
SELECT GROUP_CONCAT(name ORDER BY name SEPARATOR ', ') AS engineering_team
FROM employees
WHERE department = 'Engineering';
+--------------------------------------------------+ | engineering_team | +--------------------------------------------------+ | Dev Patel, Priya Sharma, Rohit Mehta, Sana Iqbal | +--------------------------------------------------+
(PostgreSQL and SQL Server call it STRING_AGG(name, ', ').)
Practice
Try it yourself
Find the number of products, the cheapest and most expensive price, and the average price rounded to 2 decimals.
Show solution
SELECT COUNT(*) AS products, MIN(price) AS cheapest, MAX(price) AS dearest, ROUND(AVG(price), 2) AS average_price FROM products;+----------+----------+---------+---------------+ | products | cheapest | dearest | average_price | +----------+----------+---------+---------------+ | 10 | 90.00 | 2499.00 | 835.50 | +----------+----------+---------+---------------+
How many units were sold in order 105, and what was its total?
Show solution
SELECT SUM(quantity) AS units, SUM(quantity * unit_price) AS order_total FROM order_items WHERE order_id = 105;+-------+-------------+ | units | order_total | +-------+-------------+ | 3 | 3697.00 | +-------+-------------+
Quick check
COUNT(*) vs COUNT(email)?
COUNT(*) counts all rows; COUNT(email) counts rows where email is not NULL.
What does SUM return when no rows match?
NULL – wrap it in COALESCE(…, 0) if you need 0.
Do aggregates include NULL values?
No, they skip NULLs (only COUNT(*) counts every row).
Summary
- COUNT, SUM, AVG, MIN and MAX collapse rows into one value; WHERE filters first.
- COUNT(*) counts rows, COUNT(col) skips NULLs, COUNT(DISTINCT col) counts unique values.
- Aggregates ignore NULLs and return NULL on no rows – use COALESCE when 0 is needed.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.