SQL Interview Question

What is the difference between GROUP BY and ORDER BY?

Updated 2026-07-10 · Beginner friendly
Quick answer

GROUP BY collapses rows that share a value into groups so you can run aggregates like COUNT or SUM on each group. ORDER BY simply sorts the result rows in ascending or descending order. GROUP BY changes how many rows you get, while ORDER BY only changes the order they appear in.

GROUP BY groups

It combines rows with the same value into one row per group, which is why it is almost always used with an aggregate function to summarise each group.

ORDER BY sorts

It leaves the number of rows unchanged and just arranges them, for example newest first or alphabetically.

SELECT department, COUNT(*) AS staff
FROM employees
GROUP BY department      -- one row per department
ORDER BY staff DESC;     -- sort those rows
In the interview

The one line difference is GROUP BY summarises rows into groups, ORDER BY sorts the rows. Showing that they often appear together, with GROUP BY first and ORDER BY last, demonstrates you understand query flow.

Want the full SQL guide?

Read every SQL concept with notes, diagrams, and code in one place. Track your progress as you go.

Open the SQL guide All SQL questions