SQL Quest › SQL Interview Questions › Window Functions
Top 3 Sales Per Borough
Find the top 3 DEED documents per borough by document_amt. Use RANK() OVER (PARTITION BY recorded_borough ORDER BY document_amt DESC). Show recorded_borough, document_id, document_date, document_amt, rank_in_borough. Filter to rank <= 3. Order by recorded_borough, rank.
Solve it in the browser editor →
Runs on SQLite in your browser, graded against the expected result, no signup. A wrong answer gets a diagnosis, not just "incorrect".
Schema
sales
| document_id | record_type | doc_type | recorded_borough | document_date | document_amt | recorded_datetime |
|---|---|---|---|---|---|---|
| 2025081800333001 | A | DEED | 1 | 2025-08-14 | 1080000000 | 2025-08-18 |
| 2024012300948001 | A | DEED | 1 | 2024-01-22 | 963000000 | 2024-01-24 |
| 2025081800439001 | A | DEED | 1 | 2025-08-14 | 810000000 | 2025-08-20 |
Expected output: Top 3 deals in each borough
Hint
Concepts
SELECT Window Functions RANK PARTITION BY
Practise the topic: SQL practice questions · Window function practice · Ranking function practice · Advanced SQL interview questions
Related questions
Where would this cost you points in an interview?
Ten questions, no signup: a Skillmap across nine SQL skills and the one to fix first.
Take the readiness test