Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing
Back
  1. Home
  2. Interview prep
  3. What is GROUP BY?

SQL · Aggregations & Grouping

What is GROUP BY?

Mediumsql-12
group byaggregation

Question

What is GROUP BY?

Solution

GROUP BY combines rows into groups so aggregate functions can operate on each group.

SELECT department, COUNT(*)
FROM employees
GROUP BY department;

Output might be:

IT       20
HR       10
Finance  15

If you select a normal column that isn't in GROUP BY and isn't aggregated:

SELECT department, name, COUNT(*)
FROM employees
GROUP BY department;

SQL doesn't know which employee name to return for each department.

So most SQL dialects reject it.

🎯 Put this concept into practice

Solidify this answer with real hands-on interview drills in the browser studio.

Open related drill →
PreviousNext