Ravindra BagaleCourses & study guides Track your progress

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

  1. DISTINCT cities.
  2. Top 5 orders.
  3. 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_idcityamount_inr
1016Pune1780.0
1013Pune1680.0
1007Nashik1080.0
1004Pune1050.0
1010Mumbai930.0

Meaning. Top five orders.

Ravindra Bagale's Tip

Chala mitrano — screenshot your top-5 result; explain it without looking at SQL.