TOP: Limit the Rows
Return only the first N rows with TOP — and its equivalents LIMIT and FETCH FIRST in other databases.
"Show the top 3 run-scorers." You don't need all 6 — or all 6 million — rows, just the first few after sorting. SQL Server's TOP keyword cuts the result down to a fixed number of rows.
Definition
TOP n limits the result to the first n rows. It is applied last, after the result has been sorted by ORDER BY. Without ORDER BY, which n rows you get is not defined — any n rows may come back.
Syntax
sql
SELECT TOP n column_list
FROM table_name
ORDER BY column DESC;
-- variations (SQL Server)
SELECT TOP n PERCENT column_list ... -- first n% of rows
SELECT TOP n WITH TIES column_list ... -- include rows tied with the last one (needs ORDER BY)| Part | Meaning |
|---|---|
TOP n | Keep only the first n rows |
PERCENT | Treat n as a percentage of all rows |
WITH TIES | Also return extra rows whose ORDER BY value equals the last row's |
Example — top 3 run-scorers
sql
SELECT TOP 3 *
FROM players
ORDER BY runs DESC;| id | name | team | country | runs |
|---|---|---|---|---|
| 3 | Jos Buttler | RR | England | 850 |
| 1 | Virat Kohli | RCB | India | 750 |
| 5 | David Warner | DC | Australia | 700 |
Execution order: 1️⃣ FROM → 2️⃣ SELECT * → 3️⃣ ORDER BY runs DESC → 4️⃣ TOP 3. Even though TOP is written first, it is applied last — after sorting.
Example — top 3 individual innings
sql
SELECT TOP 3 player, match_date, runs
FROM innings
ORDER BY runs DESC;| player | match_date | runs |
|---|---|---|
| Jos Buttler | 2025-04-08 | 107 |
| Virat Kohli | 2025-03-22 | 82 |
| Rohit Sharma | 2025-03-30 | 76 |
WITH TIES — fair cut-offs
In innings, Kohli and Buttler both scored 73. TOP 4 would show only one of them (whichever the engine reaches first). TOP 4 WITH TIES returns 5 rows — 107, 82, 76, 73, 73 — because both 73s tie for 4th place:
sql
SELECT TOP 4 WITH TIES player, runs
FROM innings
ORDER BY runs DESC;(WITH TIES and PERCENT are SQL Server features and don't run in the browser playground.)
SQL Server vs MySQL / PostgreSQL / Oracle
| Database | Top 3 run-scorers |
|---|---|
| SQL Server | SELECT TOP 3 * FROM players ORDER BY runs DESC; |
| MySQL / PostgreSQL / SQLite | SELECT * FROM players ORDER BY runs DESC LIMIT 3; |
| Oracle 12c+ / standard SQL | SELECT * FROM players ORDER BY runs DESC FETCH FIRST 3 ROWS ONLY; |
Note the position: TOP goes right after SELECT; LIMIT goes at the very end. SQL Server also supports the standard OFFSET … FETCH for paging (Chapter 9).
Common mistakes
- ❌
SELECT TOP 3 * FROM playerswithout ORDER BY and calling it "top 3 scorers". ✅ Always pair TOP with ORDER BY when "top" means "best". - ❌ Writing
LIMIT 3in SQL Server → syntax error. ✅ UseTOP 3. - ❌
SELECT name, TOP 3 runs→ syntax error. ✅TOPmust come immediately afterSELECT(or afterSELECT DISTINCT). - ❌ Expecting
ORDER BY runs ASC+ TOP to give the best scorers. ✅ ASC gives the lowest first — use DESC for the highest.
Interview tip
Q: How do you get the 2nd highest run-scorer?
A: One simple way: take the top 2 by runs and then pick the lower one — SELECT TOP 1 * FROM (SELECT TOP 2 * FROM players ORDER BY runs DESC) AS t ORDER BY runs ASC; — returns Virat Kohli (750). Later chapters show cleaner ways with OFFSET … FETCH and DENSE_RANK().
Key points to remember
- TOP n returns only the first n rows of the result (SQL Server syntax).
- TOP is written right after SELECT but applied LAST — after ORDER BY.
- Without ORDER BY, TOP returns an arbitrary set of n rows.
- TOP n WITH TIES also returns rows tied with the last row; TOP n PERCENT returns a percentage.
- MySQL/PostgreSQL/SQLite use LIMIT n at the end; Oracle/standard SQL use FETCH FIRST n ROWS ONLY.
- For 'best' rows, use ORDER BY ... DESC with TOP.
Try it yourself
Top 3 run-scorers
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Top 3 individual innings
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
WITH TIES and PERCENT (SQL Server)
-- Both 73-run innings tie for 4th place -> 5 rows returned
SELECT TOP 4 WITH TIES player, runs
FROM innings
ORDER BY runs DESC;
-- Top 50% of players by runs -> 3 of 6 rows
SELECT TOP 50 PERCENT name, runs
FROM players
ORDER BY runs DESC;SQL Server-specific — run this in SQL Server (SSMS / Azure Data Studio).
Practice: the 2 lowest scorers
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
