Skip to content
DataCareerHub.ioLearn · Prepare · Apply
Topic details

GROUP BY and HAVING Without Misleading the Business

Understand the idea, work through the practice, and capture what you would explain about your approach.

Concise refresherJob-ready applicationAnalytical challenge
SQL for Analysis: Application and Judgment

Lesson and practical work

free 35 minutes not started
You may already know the tool.This lesson concentrates on application, validation, and analytical judgment.

Professional outcomes

  • Aggregate metrics by dimensions at a defensible grain.
  • Explain WHERE versus HAVING in business terms.
  • Avoid double counting when grouping joined tables.
  • Interpret segment differences without ignoring size, mix, or incomplete periods.
Need a refresher?Rows first, groups secondView concise foundation

WHERE filters source rows before grouping. GROUP BY defines the dimensions of the result. HAVING filters the aggregated groups. Every selected non-aggregate field normally belongs in GROUP BY.

Find regions with material quarterly revenue

Management wants completed-order revenue and order volume by region, but only regions above $250,000 should appear in the executive review.

Filter the population, aggregate, then filter groups

SELECT
    c.region,
    COUNT(DISTINCT o.order_id) AS orders,
    SUM(o.net_revenue) AS revenue
FROM orders o
JOIN customers c ON c.customer_id = o.customer_id
WHERE o.status = 'completed'
  AND o.order_date >= '2026-04-01'
  AND o.order_date <  '2026-07-01'
GROUP BY c.region
HAVING SUM(o.net_revenue) >= 250000
ORDER BY revenue DESC;

WHERE defines eligible orders. HAVING applies the materiality threshold after regional revenue exists. Keep an unfiltered reconciliation version so excluded regions remain visible in QA.

What the result means—and does not mean

The output identifies large-revenue regions, not necessarily high-performing regions. A large region may underperform target; a small region may grow rapidly. Add comparison and scale context before ranking performance.

Analyst's checklist

  • Reconcile grouped totals before HAVING.
  • Count NULL and unknown dimension values.
  • Confirm dimension keys are unique.
  • Compare rates alongside absolute totals.
  • Check whether all periods are complete.

Common mistakes and failure modes

  • Using HAVING for ordinary row filters without reason.
  • Dropping NULL groups silently.
  • Ranking totals and calling them efficiency.
  • Grouping by a field added from a duplicated dimension.
RefresherJob-readyChallenge

Challenge yourself

A report shows the highest-revenue region last in average margin percentage.

  • Would you call it the best region?
  • Which absolute and rate measures belong together?
  • How would product mix affect interpretation?
Show hint

Scale, profitability, and target attainment answer different questions.

Show analytical guidance

Present revenue contribution, margin dollars, margin rate, and relevant targets. Then test whether product or customer mix explains the rate difference.

How this may appear in an interview

What is the difference between WHERE and HAVING?

What a strong candidate should communicate

WHERE filters rows before aggregation; HAVING filters groups after aggregation. A strong answer includes a practical example and notes that clear query design matters more than memorizing clause order.

Interviewer follow-up

Can HAVING be used without GROUP BY, and would you normally do so?

A result is usually the start of the next question

JOINs make these analyses useful across business entities, but they also create the most common source of inflated metrics.

Starter SQL

SELECT
    region,
    SUM(net_revenue) AS revenue
FROM regional_orders
WHERE /* row filters */
GROUP BY region
HAVING /* group filter */;

Test your analytical judgment

Answer before opening the guidance. More than one defensible approach may exist.

Refresher2 points

Why does a revenue threshold belong in HAVING?

Show guidance
It applies to the grouped SUM, not an individual row.
Job-ready2 points

How can grouped totals be reconciled when HAVING hides small groups?

Show guidance
Run a QA aggregate without HAVING.
Challenge2 points

Design a balanced regional performance table.

Show guidance
Combine scale, rate, target, and mix context.

Move from reviewing expert reasoning to performing the analysis.

Guided practice can add the synthetic dataset, multi-step assignment, validation checks, solution comparison, project workflow, and progress tracking.

  • Work with the data
  • Make and defend assumptions
  • Validate the result
  • Build portfolio evidence
Continue with Guided Practice
Continue your free learning path

This public lesson remains available without registration. Continue to the course outline for the next free topic.