LearnViz CompSci
Home / Learn / Databases / GROUP BY and HAVING

Beginner Topic 13 of 51 Databases

GROUP BY and HAVING

Grouping rows and filtering the groups.

How it works

GROUP BY partitions rows into groups that share the same values in the listed columns, and aggregate functions then compute one result per group. HAVING filters groups after aggregation, whereas WHERE filters rows before it. Every selected column must be either in GROUP BY or inside an aggregate. Grouping sets, ROLLUP and CUBE produce subtotals.

Step-by-step walkthrough

  1. All orders are read.
  2. Rows are grouped by customer.
  3. An aggregate such as SUM is computed per group.
  4. HAVING filters the groups, WHERE filters rows before grouping.
  5. The output has one row per surviving group.

Where it is used

Sales by region, orders per customer, error counts per hour and finding duplicates with HAVING COUNT(*) greater than one use grouping.

Common pitfall

Putting an aggregate condition in WHERE gives an error; it belongs in HAVING. Filtering early with WHERE is faster than filtering later with HAVING. Grouping by too many or unindexed columns is slow and memory hungry. Joins before grouping can multiply rows and distort counts and sums, so aggregate in a subquery first when needed.

Where it fits in the course

GROUP BY and HAVING is topic 13 of 51 in Databases, rated Beginner. It follows Aggregate Functions. The next topic is Joins.

Practice and explore

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Databases quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is GROUP BY and HAVING?
Grouping rows and filtering the groups. It belongs to Databases and is a beginner-level topic.
Where do people use GROUP BY and HAVING?
Sales by region, orders per customer, error counts per hour and finding duplicates with HAVING COUNT(*) greater than one use grouping.
What is the most common mistake with GROUP BY and HAVING?
Putting an aggregate condition in WHERE gives an error; it belongs in HAVING.
What should I learn before this topic?
Start with Aggregate Functions, the topic just before it in Databases.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Databases quiz.
Is GROUP BY and HAVING hard to learn?
It is a Beginner topic, so it works as an entry point. If the terms are new, read the explanation twice and try the visualizer before the quiz.
How long does it take to learn GROUP BY and HAVING?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to GROUP BY and HAVING?
Aggregate Functions, Joins, INSERT, UPDATE, DELETE are closely connected. The full list is in the related topics section of this page.
What should I study after GROUP BY and HAVING?
Continue with Joins, the next topic in Databases.

Related topics