CodingNeed.

2 · Summarize · 8 MIN

Group related rows

Group related rows.

GROUP BY forms one group for each distinct category. Aggregates then apply within each group. Select only grouped columns or aggregate expressions for portable, predictable queries.

Think of a spreadsheet: select columns, keep relevant rows, and summarize the result.

Read the example

SELECT category, SUM(amount) FROM expenses GROUP BY category ORDER BY category;
Check the expected output
[["food",12],["travel",3]]

Your challenge

Return each category and its row count, ordered by category.

Common trap

Row order is only guaranteed with ORDER BY.

Next lesson: Filter groups