Interview Q&A / SQL & DBMS

BeginnerQuick answer1 min readUpdated 3 Oct 2026

How does GROUP BY work with more than one column?

SHORT ANSWER

GROUP BY with several columns makes one group for each unique combination of those column values, and aggregates are computed inside each combination.

Explain it simply

Sorting cricket scores by team gives one pile per team. Sorting by team and then by year gives a pile for each team-year pair, such as India-2023 and India-2024. Multi-column GROUP BY creates piles by combinations.

Example

Table sales:

cityproductamount
PuneTea100
PuneTea150
PuneCoffee200
DelhiTea80
sql
SELECT city, product, SUM(amount) AS total
FROM sales
GROUP BY city, product
ORDER BY city, product;

Result:

cityproducttotal
DelhiTea80
PuneCoffee200
PuneTea250

Order of columns in GROUP BY does not change the groups, only possibly the default output order, which you should control with ORDER BY. Rows with NULL in a grouped column form their own group.

What to say in the interview

"GROUP BY a, b makes one group per distinct (a, b) pair. All non-aggregated selected columns must be listed. Column order does not change the grouping itself."

Common mistakes

  • Grouping by too many columns, so every group has one row and the aggregates look useless.

#sql#group-by#aggregate

Related questions

Practise more SQL & DBMS questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open SQL & DBMS quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.