Back to Chapters
Chapter Progress: 0 %0 / 6 Lessons Read
β€Ήβ€Ί

2. Summarising data: totals and groups

2.3 HAVING: filter the groups

Highlights & notes

Select lesson text to highlight it, or tap Add note.

    Bookmarks

    Bookmark a lesson to see it here.

      WHERE filters rows before grouping. HAVING filters groups after grouping. That is the whole difference.

      • Rule on a single row (status, date, city) β†’ WHERE
      • Rule on a group total (COUNT, SUM, AVG) β†’ HAVING
      • You can use both in one query

      Run order: FROM β†’ WHERE β†’ GROUP BY β†’ HAVING β†’ SELECT β†’ ORDER BY. WHERE COUNT(*) > 1 fails because the counts do not exist yet when WHERE runs.

      The duplicate check every data engineer writes

      SELECT id, COUNT(*) FROM some_table GROUP BY id HAVING COUNT(*) > 1

      If this returns any row, that id appears more than once. Run it after every load.

      Must Know:

      Filter with WHERE whenever you can. It removes rows early, so the database has less to group. Keep HAVING for conditions on aggregates.

      How it fits together

      1. WHERE: filter rows
      2. GROUP BY: make groups
      3. HAVING: filter groups

      Watch the query run

      SQL

      SELECT customer_id,       COUNT(*) AS ordersFROM ordersGROUP BY customer_idHAVING COUNT(*) >= 2ORDER BY customer_id;

      Rows at this step

      customer_id | orders
      ------------+-------
      1           | 3
      2           | 2
      3           | 2

      Practice in SQL

      Use delivered orders only. Show each product whose total amount is more than 10000, with the total as total. Biggest total first.

      Tables you can query: customers, orders
      customers
      id | name  | city
      ---+-------+------
      1  | Asha  | Noida
      2  | Ravi  | Delhi
      3  | Meera | Pune
      4  | Kabir | Noida
      5  | Zoya  | NULL
      
      orders
      id  | customer_id | product  | amount | status    | order_date
      ----+-------------+----------+--------+-----------+-----------
      101 | 1           | Laptop   | 55000  | delivered | 2026-01-05
      102 | 2           | Mouse    | 700    | delivered | 2026-01-06
      103 | 1           | Keyboard | 1500   | delivered | 2026-01-09
      104 | 3           | Monitor  | 12000  | cancelled | 2026-01-12
      105 | 2           | Laptop   | 52000  | delivered | 2026-01-15
      106 | 4           | Mouse    | 650    | pending   | 2026-01-18
      107 | 1           | Monitor  | 11500  | delivered | 2026-01-20
      108 | 3           | Keyboard | NULL   | pending   | 2026-01-22
      Expected output
      product | total
      --------+-------
      Laptop  | 107000
      Monitor | 11500

      Reference solution
      SELECT product,
             SUM(amount) AS total
      FROM orders
      WHERE status = 'delivered'
      GROUP BY product
      HAVING SUM(amount) > 10000
      ORDER BY total DESC;

      Question 1 Β· Easy

      Which clause filters groups after GROUP BY?

      Question 2 Β· Medium

      You want customers whose total spend is above 50000. Where does SUM(amount) > 50000 go?

      Question 3 Β· Hard

      What does GROUP BY id HAVING COUNT(*) > 1 find?

      Key takeaways

      WHERE filters rows before grouping; HAVING filters groups after. GROUP BY with HAVING COUNT(*) > 1 finds duplicates.

      Lesson 9 of 24

      Save your progress

      Start free and your progress, code and notes stay saved. Free for 1 month, no card needed.

      Start free Log in
      PreviousHAVING: filter the groups
      Save your progress free for 1 month. Start free Β· Log in