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 / PostgreSQL / SQLite | LIMIT 5 |
| SQL Server | SELECT TOP 5 ... |
| Standard SQL (also works on PostgreSQL, SQL Server 2012+) | FETCH FIRST 5 ROWS ONLY |
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;
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.
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;
Frequently Asked Questions
Does SQLite support TOP or FETCH FIRST too?
No, SQLite only supports LIMIT. Of the four databases in this course, SQL Server is the only one that doesn't support LIMIT at all, and uses TOP or FETCH FIRST instead.
Can I combine LIMIT with an offset to skip rows, for pagination?
Yes, this course covers that in depth in a later lesson, each database's LIMIT/TOP/FETCH FIRST syntax has a matching way to skip a number of rows first.
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 four 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 four databases you're targeting before writing LIMIT, TOP, or FETCH FIRST, 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.
Summary
This lesson covered the three ways to limit a query's row count across databases, LIMIT, TOP, and FETCH FIRST, and why pairing any of them with ORDER BY is what actually makes a row limit meaningful.
That completes Module 1, SQL Basics: writing a query, filtering, sorting, inserting, updating, deleting, handling NULL, and limiting results, the core toolkit for reading and writing data in any SQL database.
What's Next?
Module 2, Operators and Filtering, is next: AND, OR, NOT, LIKE, wildcards, IN, BETWEEN, and aliases.
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.