12. GROUP BY: One Row Per Group
12.5 Common GROUP BY Mistakes (Missing Column in GROUP BY)
Why. The classic error: selected a column not in GROUP BY and not aggregated.
What. Every selected column must be aggregated or listed in GROUP BY.
How. Fix by adding the column to GROUP BY or wrapping it in an aggregate.
Steps
- See the wrong pattern (comment in your notes).
- Fix with GROUP BY city.
- Know SQLite can be lenient — other engines are strict.
Examples
Example 1 · dataset: raja_coffee
SELECT city, COUNT(*) AS n
FROM orders
GROUP BY city
ORDER BY n DESC;Output
| city | n |
|---|---|
| Pune | 10 |
| Nashik | 3 |
| Mumbai | 3 |
| Kolhapur | 3 |
| Goa | 3 |
| Solapur | 2 |
Meaning. Correct pattern: city is in GROUP BY.
Ravindra Bagale's Tip
Interview: explain why SELECT city, customer_id, SUM(amount) needs both keys in GROUP BY or a join redesign.
Ravindra Bagale's Tip – मराठी
Interview: city, customer_id, SUM साठी दोन्ही कीज GROUP BY हव्यात किंवा join पुनर्लेखन.
Ravindra Bagale's Tip – हिंदी
इंटरव्यू: city, customer_id, SUM के लिए दोनों कुंजी GROUP BY में या join फिर से।