Module 03 · Lesson 6 of 13Detailed study · about 15 min

Summarize groups with GROUP BY and HAVING

GROUP BY combines rows into groups. Aggregate functions summarize each group; HAVING filters the summaries.

Understand the idea

First decide the grain of the result: one row per region, per day or per customer. Every non-aggregated selected expression must be compatible with the grouping. Adding more grouping columns changes the question.

Key ideas in simple English
ConceptWhat it means
WHEREFilters individual rows before grouping.
GROUP BYDefines the grouping keys.
SUM / COUNT / AVGCalculate totals, counts and averages.
HAVINGFilters groups using their calculated values.

A concrete example

East remains with ORDERS 2, KNOWN_AMOUNTS 2 and SALES 100. West has two rows but only one known amount and total 30, so HAVING excludes it.

Remember: Use HAVING for the group total; WHERE operates before the groups exist.

Explain it step by step · full theory

Understand the idea

First decide the grain of the result: one row per region, per day or per customer. Every non-aggregated selected expression must be compatible with the grouping. Adding more grouping columns changes the question.

COUNT(*) counts rows, while COUNT(column) counts non-NULL values in that column. AVG also ignores NULL inputs. A missing amount should not silently be treated as a real zero unless that is your business rule.

Words used in this lesson
WHERE
Filters individual rows before grouping.
GROUP BY
Defines the grouping keys.
SUM / COUNT / AVG
Calculate totals, counts and averages.
HAVING
Filters groups using their calculated values.

See how it works

Choose a step to explore the idea at your own pace.

Open the interactive explanation
Interactive explainer1 / 3

Summarize groups with GROUP BY and HAVING

Sample rows
Question

Choose the input

GROUP BY combines rows into groups. Aggregate functions summarize each group; HAVING filters the summaries.

Automatic playback is off for reduced motion. Select any step or use Previous and Next. Illustrations explain behavior; they are not live measurements.

Read the complete diagram explanation
  1. Choose the input

    GROUP BY combines rows into groups. Aggregate functions summarize each group; HAVING filters the summaries.

  2. Apply the SQL

    First decide the grain of the result: one row per region, per day or per customer. Every non-aggregated selected expression must be compatible with the grouping. Adding more grouping columns changes the question.

  3. Explain the result

    Use HAVING for the group total; WHERE operates before the groups exist.

Try the SQL or command

Read → predict → run in your learning environment → compare. You can also learn by reading without an account.

New to SQL? Where to run it and how to read it

A query is a question written in SQL. Snowflake runs it and returns a result table. This website shows examples; it does not connect to your account.

  1. No account yet? Read and predict the output first. Follow the account and Snowsight lesson when you are ready.
  2. For SQL practice, use a learning account in Snowsight and select an authorized role and warehouse. A role determines permissions; a warehouse supplies processing power. Follow the example’s database/schema setup when it refers to stored objects.
  3. Read the entire example, including comments beginning with --. Run the selected statement or labeled block in the stated order. Examples labeled Python, shell or HTTP belong in that client, not in a SQL editor.
  4. Compare values and row counts with the explained result. If an example deliberately causes an error, follow its rollback and cleanup steps before continuing.
SELECT / FROM
Choose the values to return / identify their input.
VALUES / COLUMN1
VALUES supplies tiny example rows. COLUMN1 means their first column, COLUMN2 their second.
AS / WITH
AS gives a result a readable name. WITH names a temporary query step within one statement.
NULL / ::
NULL means missing or unknown. The :: notation converts a value to a named data type.
Session
One connection and its settings. Keep related transaction steps in the same session.
Database.schema.table
A full address for stored data. It is separate from the warehouse that processes queries.

Need the L2_OBJECTS_LAB setup? Open the first database-object lesson →

Help with common practice errors
  • No active warehouse: select a warehouse your role can use. Do not assume you need to create a new one.
  • Object missing or not authorized: check the full name, required setup and active role. Ask the account owner for access if needed.
  • Object already exists: setup may already have run. Read the current state or choose a fresh lab name consistently; do not add OR REPLACE blindly.
  • Different result: check whether an earlier INSERT or UPDATE changed the data. VALUES examples provide their own fresh input for each statement.

Before running this lesson

Use an authorized learning account and warehouse. VALUES examples need no stored tables. Object-changing examples use separate disposable tables in L2_OBJECTS_LAB.SALES from the first lesson; run each setup once with a fresh name.

Find regions with enough sales

SQL · example
WITH orders AS (SELECT COLUMN1 AS REGION, COLUMN2 AS AMOUNT
FROM VALUES ('East',40),('East',60),('West',30),('West',NULL))
SELECT REGION, COUNT(*) AS ORDERS, COUNT(AMOUNT) AS KNOWN_AMOUNTS,
 SUM(AMOUNT) AS SALES FROM orders
GROUP BY REGION HAVING SUM(AMOUNT) >= 50 ORDER BY REGION;

What to expect and why

East remains with ORDERS 2, KNOWN_AMOUNTS 2 and SALES 100. West has two rows but only one known amount and total 30, so HAVING excludes it.

Check your understanding

Choose an answer, then check your reasoning. This is practice, not a scored test.

Where should a condition on SUM(AMOUNT) go?

Try it yourself

Where should a condition on SUM(AMOUNT) go?

Compare your reasoning

Use HAVING for the group total; WHERE operates before the groups exist.

Official sources and curriculum

Module 03 · See module scope