Skip to content
Interview prep/SQL GROUP BY Interview Questions
Interview practice

SQL GROUP BY Interview Questions

Practice counts, sums, averages, HAVING, and grouped business reports.

GROUP BY questions test whether you can turn rows into a business summary without losing the meaning of each row. The main decision is the output grain: one row per category, customer, month, channel, movie, or supplier.

Once you know the grain, every selected column must either define that grain or be wrapped in an aggregate such as count, sum,avg, min, or max.

Common GROUP BY interview prompts

  1. Count products by category and include average price and total units on hand.
  2. Return customers with at least $75 in shipped revenue.
  3. Find the top five pages by pageviews.
  4. Calculate sessions and average duration by marketing channel.
  5. Explain the difference between WHERE and HAVING.
Category report
select c.category_name,
       count(*) as products,
       round(avg(p.price), 2) as avg_price,
       sum(p.quantity_on_hand) as units_on_hand
from products as p
join categories as c on c.category_id = p.category_id
group by c.category_name
having count(*) >= 2
order by products desc, c.category_name

What matters

A product-by-category report groups on category_name and then aggregates the product rows inside each category. If you addproduct_name to the SELECT list, the query no longer has one row per category unless you also group by product name.

The HAVING clause filters groups after aggregation. UseWHERE for row-level filters such as shipped orders or a date range; use HAVING for aggregate filters such as customers with revenue above a threshold.

Aggregate filter
select c.customer_id,
       c.first_name,
       c.last_name,
       round(sum(oi.quantity * oi.unit_price), 2) as revenue
from customers as c
join orders as o on o.customer_id = c.customer_id
join order_items as oi on oi.order_id = o.order_id
where o.status = 'shipped'
group by c.customer_id, c.first_name, c.last_name
having sum(oi.quantity * oi.unit_price) >= 75
order by revenue desc

How to talk it through

Start with the shape: "This report is one row per customer, so I will group by the customer identifier and display columns. I will filter to shipped orders in WHERE before calculating revenue, then use HAVING to keep only customers whose aggregate revenue meets the threshold."

If the interviewer asks for a percentage or rate, call out integer division. Use 100.0 or an explicit decimal cast when the engine would otherwise truncate integer division.

Practice next

Review GROUP BY, HAVING, and the GROUP BY error guide. Then try therevenue audit missionand movie genre scorecard.

Quick answers

What is the first decision in a GROUP BY interview problem?

Decide the output grain, such as one row per customer, category, month, or channel. That grain determines the GROUP BY columns.

When should I use HAVING instead of WHERE?

Use WHERE for row-level filters before aggregation and HAVING for filters that depend on aggregate values after grouping.

Why do GROUP BY errors happen?

They happen when the SELECT list includes a column that is neither grouped nor aggregated, so the database cannot choose one value for the group.