Interactive SQL practice

GROUP BY Exercises

Build grouped reports with COUNT, SUM, AVG, GROUP BY and HAVING. These 38 interactive challenges use real schemas and instant query validation.

Practice this concept

Start with a free exercise

Count Employees Per DepartmentMore exercises on this topic

38 SQL challenges

Open Practice Arena
GROUP BY
Beginner

Customers per Country

Count customers in each country, sorted by the country name. Return columns: country, customer_count. Order results by: country.

GROUP BY
Intermediate

Order Status Summary

Show the order count and total amount for each status. Return columns: status, order_count, total_amount. Order results by: status.

HAVING
Intermediate

Repeat Customers

Find customers who placed more than one order. Return columns: customer_name, order_count. Order results by: order_count DESC, customer_name.

HAVING
Advanced
PRO

Customers Spending Above 750

Find customers whose total completed-order spend exceeds 750. Return columns: customer_name, total_spend. Order results by: total_spend DESC, customer_name.

HAVING
Advanced
PRO

Customers Spending Above 900

Find customers whose total completed-order spend exceeds 900. Return columns: customer_name, total_spend. Order results by: total_spend DESC, customer_name.

Concept guide

How to practice GROUP BY

GROUP BY turns detailed rows into grouped summaries. It is commonly paired with COUNT, SUM, AVG, MIN, MAX and HAVING for reporting questions.

What to master

  • • Group by every selected non-aggregate expression
  • • Use WHERE before grouping and HAVING after grouping
  • • Check how NULL values form groups
  • • Order summaries explicitly when ranking groups

A reliable problem-solving workflow

  1. 1Decide the grain of one output row.
  2. 2Select the grouping columns.
  3. 3Add the required aggregate functions.
  4. 4Apply row filters with WHERE and aggregate filters with HAVING.
What is the difference between WHERE and HAVING?

WHERE filters source rows before grouping. HAVING filters the grouped results after aggregate functions are calculated.

Can GROUP BY be used without an aggregate function?

Yes, but it usually behaves like a distinct list of grouping values. DISTINCT is often clearer when no aggregation is required.