Ravindra BagaleCourses & study guides Track your progress

12. GROUP BY: One Row Per Group

12.2 How to Group by One Column (Sales by City)

Why. One-column groups answer “by city / by channel / by agent”.

What. GROUP BY one column + aggregates on measures.

How. SELECT city, COUNT(*), SUM(amount_inr) FROM orders GROUP BY city;

Steps

  1. Group orders by city.
  2. Group sales by currency.
  3. Alias aggregates clearly.

Examples

Example 1 · dataset: interview_mini

SELECT channel, COUNT(*) AS n,
       ROUND(SUM(amount),2) AS total
FROM sales
GROUP BY channel
ORDER BY total DESC;

Output

channelntotal
Store53365.0
Online41225.0
3650.0

Meaning. Note NULL channel becomes its own group.

Ravindra Bagale's Tip

NULL group key = its own group — expect a blank channel row.