5. ORDER BY, DISTINCT and Top-N Results
5.6 Practice: Top Orders and Unique Cities
Why. Combine sort, distinct and top-n like a mini interview set.
What. Practice on raja_coffee until the patterns feel automatic.
How. Three tasks: unique cities, top orders, two-column sort.
Steps
- DISTINCT cities.
- Top 5 orders.
- Sort city then amount.
Examples
Example 1 · dataset: raja_coffee
SELECT DISTINCT city FROM orders ORDER BY city;Output
| city |
|---|
| Goa |
| Kolhapur |
| Mumbai |
| Nashik |
| Pune |
| Solapur |
Meaning. Unique cities list.
Example 2 · dataset: raja_coffee
SELECT order_id, city, amount_inr FROM orders ORDER BY amount_inr DESC LIMIT 5;Output
| order_id | city | amount_inr |
|---|---|---|
| 1016 | Pune | 1780.0 |
| 1013 | Pune | 1680.0 |
| 1007 | Nashik | 1080.0 |
| 1004 | Pune | 1050.0 |
| 1010 | Mumbai | 930.0 |
Meaning. Top five orders.
Ravindra Bagale's Tip
Chala mitrano — screenshot your top-5 result; explain it without looking at SQL.
Ravindra Bagale's Tip – मराठी
चला मित्रांनो — top-5 चा screenshot; SQL न बघता समजावून सांगा.
Ravindra Bagale's Tip – हिंदी
चलो दोस्तों — top-5 का स्क्रीनशॉट; SQL देखे बिना समझाएँ।