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
- Group orders by city.
- Group sales by currency.
- 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
| channel | n | total |
|---|---|---|
| Store | 5 | 3365.0 |
| Online | 4 | 1225.0 |
| 3 | 650.0 |
Meaning. Note NULL channel becomes its own group.
Ravindra Bagale's Tip
NULL group key = its own group — expect a blank channel row.
Ravindra Bagale's Tip – मराठी
NULL ग्रुप की = स्वतःचा ग्रुप — रिकाम्या channel रोची अपेक्षा.
Ravindra Bagale's Tip – हिंदी
NULL ग्रुप कुंजी = अपना ग्रुप — खाली channel पंक्ति अपेक्षित।