Aral Code

Functions & Grouping Lesson 12 of 21

SQL GROUP BY and HAVING

Summarize data per group with GROUP BY — products per category, orders per customer, average per subject — and filter groups with HAVING.

Aggregate functions give one answer for the whole table. But often you want one answer per group: the number of products in each category, the average score in each subject. That's what GROUP BY does.

Counting per group

SQL Number of products in each category
SELECT category, COUNT(*) AS products
FROM products
GROUP BY category;

The database puts the rows into piles, one pile per category, and runs COUNT(*) on each pile.

Other aggregates per group

SQL Average score per subject
SELECT subject,
       COUNT(*) AS students,
       ROUND(AVG(score), 1) AS average,
       MAX(score) AS highest
FROM grades
GROUP BY subject;
SQL Stock value per category, highest first
SELECT category, SUM(price * stock) AS stock_value
FROM products
GROUP BY category
ORDER BY stock_value DESC;

The golden rule of GROUP BY

Every column in SELECT must be either:

  1. in the GROUP BY list, or
  2. inside an aggregate function like COUNT, SUM or AVG.

Grouping by several columns

SQL Students per grade level and section
SELECT grade, section, COUNT(*) AS students
FROM students
GROUP BY grade, section
ORDER BY grade, section;

Orders per customer

SQL How many orders each customer made
SELECT customer_id, COUNT(*) AS orders
FROM orders
GROUP BY customer_id
ORDER BY orders DESC;

Customer 1 (Maria Santos) is the most loyal. In the JOIN lessons, you'll show names instead of ids.

HAVING: filtering groups

WHERE filters rows before grouping. HAVING filters groups after grouping, so it can use aggregate functions:

SQL Categories with more than one product
SELECT category, COUNT(*) AS products
FROM products
GROUP BY category
HAVING COUNT(*) > 1;
SQL Customers with 2 or more orders
SELECT customer_id, COUNT(*) AS orders
FROM orders
GROUP BY customer_id
HAVING COUNT(*) >= 2;

WHERE and HAVING together

SQL Scores of 80+ only, subjects averaging over 88
SELECT subject, ROUND(AVG(score), 1) AS avg_of_80_plus
FROM grades
WHERE score >= 80
GROUP BY subject
HAVING AVG(score) > 88;

Step by step: WHERE removes scores below 80, GROUP BY makes one pile per subject, HAVING keeps only subjects whose average is above 88.

The full order of clauses

SELECT    columns, aggregates
FROM      table
WHERE     row conditions
GROUP BY  columns
HAVING    group conditions
ORDER BY  columns
LIMIT     number;

A popular memory trick: Some Friendly Workers Go Home On Lunch.

Best-selling products

SQL Total quantity sold per product (top 5)
SELECT product_id, SUM(qty) AS sold
FROM order_items
GROUP BY product_id
ORDER BY sold DESC
LIMIT 5;

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What does GROUP BY do?
2. Which clause can filter on COUNT(*) > 2?
3. Which clause comes first?
4. Why is SELECT category, name, COUNT(*) ... GROUP BY category a problem?

By Sir Reyel · Last updated 2026-12-04