TExcelTAD
Home › SQL › SQL GROUP BY

SQL GROUP BY

GROUP BY sorts rows into groups that share the same value(s) and lets aggregate functions — COUNT, SUM, AVG, MIN, MAX — compute one result per group.

sql
SELECT column, AGGREGATE(other_column)
FROM table_name
WHERE condition
GROUP BY column
ORDER BY ...;

Customers per country

sql · try it yourself
SELECT country, COUNT(*) AS customers
FROM customers
GROUP BY country
ORDER BY customers DESC, country
LIMIT 6;
country   | customers
----------+----------
Canada    | 2
France    | 2
Germany   | 2
UK        | 2
USA       | 2
Australia | 1
(6 rows)

Conceptually, the database puts all customer rows with the same country into one bucket, then counts each bucket.

Several aggregates per group

sql · try it yourself
SELECT category_id,
       COUNT(*)   AS products,
       SUM(stock) AS units,
       MIN(price) AS cheapest,
       MAX(price) AS dearest
FROM products
GROUP BY category_id
ORDER BY category_id;
category_id | products | units | cheapest | dearest
------------+----------+-------+----------+--------
1           | 5        | 267   | 3.2      | 24.5
2           | 4        | 79    | 4.9      | 22
3           | 3        | 194   | 1.45     | 7.8
4           | 4        | 340   | 1.89     | 4.5
5           | 3        | 349   | 2.49     | 5.99
6           | 5        | 222   | 9.99     | 149
7           | 3        | 34    | 38       | 59.9
8           | 3        | 102   | 29       | 39
(8 rows)

Grouping with a join

Group by the category name instead of its id by joining first:

sql · try it yourself
SELECT c.name AS category, SUM(oi.quantity) AS units_sold
FROM order_items AS oi
JOIN products   AS p ON p.product_id = oi.product_id
JOIN categories AS c ON c.category_id = p.category_id
GROUP BY c.category_id, c.name
ORDER BY units_sold DESC;
category    | units_sold
------------+-----------
Beverages   | 53
Electronics | 51
Bakery      | 38
Produce     | 37
Snacks      | 29
Home        | 28
Dairy       | 23
Books       | 20
(8 rows)

Grouping by several columns

Each distinct combination forms a group. Orders per employee and status:

sql · try it yourself
SELECT employee_id, status, COUNT(*) AS orders
FROM orders
WHERE employee_id IN (5, 9)
GROUP BY employee_id, status
ORDER BY employee_id, status;
employee_id | status     | orders
------------+------------+-------
5           | cancelled  | 2
5           | delivered  | 3
5           | shipped    | 1
9           | cancelled  | 1
9           | delivered  | 3
9           | processing | 3
(6 rows)

Grouping by an expression

You can group by a calculated value. substr(order_date, 1, 7) extracts YYYY-MM, so this counts orders per month:

sql · try it yourself
SELECT substr(order_date, 1, 7) AS month, COUNT(*) AS orders
FROM orders
GROUP BY month
ORDER BY month;
month   | orders
--------+-------
2025-01 | 4
2025-02 | 4
2025-03 | 4
2025-04 | 4
2025-05 | 4
2025-06 | 4
2025-07 | 4
2025-08 | 4
2025-09 | 4
2025-10 | 4
(10 rows)

The order of clauses

text
SELECT … FROM … WHERE … GROUP BY … HAVING … ORDER BY … LIMIT …

WHERE filters rows before grouping; HAVING (next lesson) filters groups after grouping.

The golden rule

Every column in the SELECT list must either be in GROUP BY or be inside an aggregate function. SELECT country, name, COUNT(*) … GROUP BY country is an error in PostgreSQL and SQL Server because there are many names per country — which one should be shown? SQLite and MySQL (with some settings) accept it and pick an arbitrary row, which is rarely what you want.

Tip: If you group by an id, it is fine to also group by (or select) columns that depend on it, like c.category_id, c.name. PostgreSQL even allows selecting c.name after grouping only by the primary key.

Test yourself

What does GROUP BY country with COUNT(*) return?

  1. One row per customer
  2. One row per country with the number of rows in it
  3. The number of countries
  4. Only countries with more than one row

Which clause filters rows before they are grouped?

  1. HAVING
  2. WHERE
  3. ORDER BY
  4. LIMIT

With GROUP BY employee_id, status, what is a group?

  1. Each employee
  2. Each status
  3. Each combination of employee and status
  4. Each order