SQL LIMIT / TOP / FETCH FIRST
These keywords let you limit how many rows your query returns. Usage differs slightly by database. πΉ Syntax by DBMS πΉ Pagination Example (MySQL/PostgreSQL) π§ Quick Recap DBMS Keyword Used Notes MySQL LIMIT With optional OFFSET for paging PostgreSQL LIMIT / OFFSET or FETCH Both options supported SQL Server TOP Use TOP N in SELECT […]
These keywords let you limit how many rows your query returns. Usage differs slightly by database.
πΉ Syntax by DBMS
-- β
MySQL / PostgreSQL
SELECT * FROM employees
ORDER BY salary DESC
LIMIT 5;
-- β
SQL Server
SELECT TOP 5 * FROM employees
ORDER BY salary DESC;
-- β
Oracle 12c+ / PostgreSQL (Standard SQL)
SELECT * FROM employees
ORDER BY salary DESC
FETCH FIRST 5 ROWS ONLY;πΉ Pagination Example (MySQL/PostgreSQL)
-- Get 10 rows starting from row 21 (page 3 if page size is 10)
SELECT * FROM employees
ORDER BY name
LIMIT 10 OFFSET 20;π§ Quick Recap
| DBMS | Keyword Used | Notes |
|---|---|---|
| MySQL | LIMIT | With optional OFFSET for paging |
| PostgreSQL | LIMIT / OFFSET or FETCH | Both options supported |
| SQL Server | TOP | Use TOP N in SELECT |
| Oracle 12c+ | FETCH FIRST | Use with ORDER BY for proper results |
β
Use this to show only top results or paginate large datasets
Was this helpful?
Thanks for your feedback!