Sometimes you only want a handful of rows, the top 5 highest earners, a quick preview of a table. Capping how many rows a query returns is one of the few places this course has to show genuinely different syntax for different databases, there's no single keyword that works everywhere.
Learning Objectives
- Limit a query's results using the correct keyword for a given database.
- Combine a row limit with ORDER BY to get a meaningful "top N."
- Recognize LIMIT, TOP, and FETCH FIRST as the same underlying idea in different syntax.
Full Result
Every matching row
ORDER BY
Meaningful order first
LIMIT / TOP / FETCH FIRST
Row count capped
Top N Rows
Exactly what you asked for
Limiting Rows by Database
| Database | Syntax |
|---|---|
| MySQL / MariaDB / PostgreSQL / SQLite | LIMIT 5 |
| SQL Server | SELECT TOP 5 ... |
| Oracle (12c+, 2013) / PostgreSQL / SQL Server (2012+) | FETCH FIRST 5 ROWS ONLY |
| Oracle (pre-12c) | WHERE ROWNUM <= 5 (needs a wrapping subquery to combine with ORDER BY) |
SELECT first_name, salary FROM employees ORDER BY salary DESC LIMIT 5;
SELECT TOP 5 first_name, salary FROM employees ORDER BY salary DESC;
SELECT first_name, salary FROM employees ORDER BY salary DESC FETCH FIRST 5 ROWS ONLY;
SELECT first_name, salary FROM ( SELECT first_name, salary FROM employees ORDER BY salary DESC ) WHERE ROWNUM <= 5;
Notice TOP goes right after SELECT, while LIMIT and FETCH FIRST go at the very end of the query, a real structural difference, not just a different keyword in the same spot. Oracle's older ROWNUM approach needs a query nested inside another query (a "subquery," covered in full in Module 6), ROWNUM is assigned to rows before any ordering happens, so filtering ROWNUM <= 5 before sorting would cap the wrong 5 rows. Don't worry about fully understanding the nested-query syntax yet, just note that this is how older Oracle handles it, and move on.
Always Pair With ORDER BY
LIMIT 5 (or TOP 5, or FETCH FIRST 5 ROWS ONLY) on its own just returns some 5 rows, with no guarantee which ones, the same unpredictable-order issue covered in the ORDER BY lesson. Pair it with ORDER BY to get a meaningful "top N," as in every example above.
Try It Yourself
Write a query returning just the 2 lowest-paid employees, for MySQL and for SQL Server.
SELECT first_name, salary FROM employees ORDER BY salary ASC LIMIT 2;
SELECT TOP 2 first_name, salary FROM employees ORDER BY salary ASC;
SELECT first_name, salary FROM employees ORDER BY salary ASC FETCH FIRST 2 ROWS ONLY;
Frequently Asked Questions
Does SQLite support TOP or FETCH FIRST too?
No, SQLite only supports LIMIT, same as MySQL and MariaDB. SQL Server is the only one of the six databases in this course that doesn't support LIMIT at all, it uses TOP or FETCH FIRST (2012+) instead. Oracle supports FETCH FIRST from 12c onward, and the older ROWNUM technique on anything before that.
Can I combine LIMIT with an offset to skip rows, for pagination?
Yes, the next lesson covers OFFSET in full, skipping a number of rows before the limit applies, the basis of pagination.
Common Beginner Mistakes
Using LIMIT syntax on SQL Server, or TOP syntax on MySQL
Neither works, they're database-specific keywords. Check which of the six databases you're actually targeting before copying a row-limiting query from a tutorial.
Using LIMIT without ORDER BY and assuming it returns the "first" or "best" rows
Without ORDER BY, which rows come back is not guaranteed, the database returns whatever is most efficient internally. LIMIT only becomes meaningful paired with an explicit sort.
Best Practices
- Always pair a row limit with ORDER BY, unless you genuinely don't care which rows you get.
- Know which of the six databases you're targeting before writing LIMIT, TOP, FETCH FIRST, or ROWNUM, they aren't interchangeable.
- Prefer FETCH FIRST when writing SQL meant to be portable across PostgreSQL and SQL Server, since both support it, unlike LIMIT or TOP individually.
Interview Questions
Write a query returning the 3 highest-paid employees, for both MySQL and SQL Server.
MySQL: SELECT first_name, salary FROM employees ORDER BY salary DESC LIMIT 3; SQL Server: SELECT TOP 3 first_name, salary FROM employees ORDER BY salary DESC; —same intent, different keyword placement.
Why does SQL Server not support LIMIT?
It's simply a different vendor's design choice from early on, SQL Server adopted TOP instead, and later added FETCH FIRST support (from SQL Server 2012) as the database industry moved toward that ANSI-standard phrasing, but it never added LIMIT itself.
Why does Oracle's older ROWNUM technique require wrapping the query in a subquery, instead of just adding WHERE ROWNUM <= 5 directly?
ROWNUM is assigned to each row as it's produced, before ORDER BY has sorted anything. WHERE ROWNUM <= 5 directly on the base query would cap 5 arbitrary, unsorted rows and then sort only those. Wrapping the already-sorted query in a subquery and applying ROWNUM <= 5 to that outer query correctly caps the already-ordered top 5.
Summary
This lesson covered the different ways to limit a query's row count across all six databases, LIMIT (MySQL, MariaDB, PostgreSQL, SQLite), TOP (SQL Server), FETCH FIRST (PostgreSQL, SQL Server 2012+, Oracle 12c+), and Oracle's older ROWNUM technique, and why pairing any of them with ORDER BY is what actually makes a row limit meaningful.
What's Next?
The next lesson covers OFFSET, skipping a number of rows before the limit applies, the other half of pagination.
Ready to Practice SQL?
Write and run real SQL queries online, right in your browser, no install or signup required. Pick a playground based on which database you want to practice on.
Both open a third-party SQL playground in a new tab. External site terms and privacy policy apply.