Tools

SQL LIMIT / TOP / FETCH FIRST

Share:
Article Summary

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

DBMSKeyword UsedNotes
MySQLLIMITWith optional OFFSET for paging
PostgreSQLLIMIT / OFFSET or FETCHBoth options supported
SQL ServerTOPUse TOP N in SELECT
Oracle 12c+FETCH FIRSTUse with ORDER BY for proper results

βœ… Use this to show only top results or paginate large datasets

Was this helpful?

Written by

W3buddy
W3buddy

Explore W3Buddy for in-depth guides, breaking tech news, and expert analysis on AI, cybersecurity, databases, web development, and emerging technologies.