A city-operations review opens with the three biggest revenue markets, strongest first.
From city_sales, return city and total_revenue, restricted to the top 3 cities by revenue and ordered from highest to lowest. Row order matters for this question.
Sample data
Table: city_sales
16 row(s) · showing first 8
| sale_idINT | cityTEXT | sale_dateDATE | revenueNUMERIC(10,2) |
|---|---|---|---|
| 1 | Bengaluru | 2026-01-03 | 185000.00 |
| 2 | Bengaluru | 2026-01-17 | 162000.00 |
| 3 | Bengaluru | 2026-02-02 | 174500.00 |
| 4 | Mumbai | 2026-01-05 | 210000.00 |
| 5 | Mumbai | 2026-01-19 | 198000.00 |
| 6 | Mumbai | 2026-02-04 | 221000.00 |
| 7 | Delhi | 2026-01-07 | 176000.00 |
| 8 | Delhi | 2026-01-21 | 169000.00 |
city_sales
- sale_idINT
- cityTEXT
- sale_dateDATE
- revenueNUMERIC(10,2)
- Step 1: Aggregate transactions to city level with `GROUP BY city` and `SUM(revenue)`.
- Step 2: Sort the aggregated rows with `ORDER BY total_revenue DESC`. Postgres allows referencing the alias in `ORDER BY` because it runs after the `SELECT` list is projected.
- Step 3: Cut the list with `LIMIT 3`. `LIMIT` is applied after sorting, so it genuinely gives the top three.
- Step 4: Keep the `ORDER BY` in the final query — the result is a ranked list, so row order is part of the answer.
SELECT city, SUM(revenue) AS total_revenue
FROM city_sales
GROUP BY city
ORDER BY total_revenue DESC
LIMIT 3;Learn the concept first
Stuck on Aggregate Functions? These free SQL lessons teach it step by step.
More Aggregate Functions questions
Practising alone only goes so far — our live cohorts pair SQL depth with dashboards, agentic analytics projects and mock interviews.
Explore the program