SQL Quest › SQL Interview Questions › Window Functions

Top 3 Sales Per Borough

HardProQuerying BasicsWindow Functions

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_idrecord_typedoc_typerecorded_boroughdocument_datedocument_amtrecorded_datetime
2025081800333001ADEED12025-08-1410800000002025-08-18
2024012300948001ADEED12024-01-229630000002024-01-24
2025081800439001ADEED12025-08-148100000002025-08-20

Expected output: Top 3 deals in each borough

Hint

This is a Pro challenge — the hint, the step-by-step tutor and the reference solution open in the app.

Concepts

SELECT Window Functions RANK PARTITION BY

Practise the topic: SQL practice questions · Window function practice · Ranking function practice · Advanced SQL interview questions

Read the concept: SQL running total

Related questions

Earliest Movie per GenreHard · ProTop 3 Banks Per State by AssetsHard · ProQoQ Asset Growth — Top BanksHard · ProYoY Sales Volume Per BoroughHard · ProTop 5 Torque Per QualityHard · ProRolling Tool Wear TrendHard · Pro

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