Why Limit Rows?
Limiting rows is useful for previewing data quickly, building paginated results, finding top-N values, and improving query performance on large tables.
We will use this Employees table:
+----+-----------+------------+--------+-----------+
| ID | Name | Department | Salary | City |
+----+-----------+------------+--------+-----------+
| 1 | Priya K. | Sales | 55000 | Mumbai |
| 2 | John D. | Marketing | 62000 | London |
| 3 | Arun M. | IT | 78000 | Delhi |
| 4 | Emma W. | Sales | 51000 | Sydney |
| 5 | Ravi S. | IT | 72000 | Mumbai |
| 6 | Sara L. | HR | 58000 | New York |
+----+-----------+------------+--------+-----------+
MySQL / PostgreSQL — LIMIT
The LIMIT clause restricts how many rows are returned. It goes at the very end of the query:
SELECT Name, Salary
FROM Employees
ORDER BY Salary DESC
LIMIT 3;
+-----------+--------+ | Name | Salary | +-----------+--------+ | Arun M. | 78000 | | Ravi S. | 72000 | | John D. | 62000 | +-----------+--------+
SQL Server — TOP
SQL Server uses TOP instead of LIMIT. It goes right after SELECT:
SELECT TOP 3 Name, Salary
FROM Employees
ORDER BY Salary DESC;
Oracle / Standard SQL — FETCH FIRST
Oracle and the SQL standard use FETCH FIRST:
SELECT Name, Salary
FROM Employees
ORDER BY Salary DESC
FETCH FIRST 3 ROWS ONLY;
Syntax Comparison
| Database | Syntax | Position |
|---|---|---|
| MySQL, PostgreSQL, SQLite | LIMIT n |
End of query |
| SQL Server, MS Access | SELECT TOP n |
After SELECT |
| Oracle, DB2, Standard SQL | FETCH FIRST n ROWS ONLY |
End of query |
LIMIT with OFFSET — Pagination
OFFSET skips a specified number of rows before returning results. Combined with LIMIT, it enables pagination:
SELECT Name, Salary
FROM Employees
ORDER BY ID
LIMIT 3 OFFSET 2;
+-----------+--------+ | Name | Salary | +-----------+--------+ | Arun M. | 78000 | | Emma W. | 51000 | | Ravi S. | 72000 | +-----------+--------+
OFFSET 2 skips the first 2 rows (Priya, John). LIMIT 3 returns the next 3 rows.
💡 Pagination Formula
For page-based results: LIMIT page_size OFFSET (page_number - 1) * page_size. For example, page 2 with 10 rows per page: LIMIT 10 OFFSET 10.
LIMIT with WHERE
You can combine LIMIT with WHERE to get the top results from a filtered set. Always use ORDER BY with LIMIT for predictable results:
SELECT Name, Department, Salary
FROM Employees
WHERE Salary > 55000
ORDER BY Salary DESC
LIMIT 2;
+-----------+------------+--------+ | Name | Department | Salary | +-----------+------------+--------+ | Arun M. | IT | 78000 | | Ravi S. | IT | 72000 | +-----------+------------+--------+
⚠️ Always Use ORDER BY with LIMIT
Without ORDER BY, the database returns rows in an unpredictable order. LIMIT without ORDER BY gives you random rows each time. Always sort first, then limit, to get consistent and meaningful results.
For a complete reference of the LIMIT syntax, see the official MySQL SELECT documentation. In the next chapter, you will learn how to find minimum and maximum values with SQL MIN and MAX.
If you work with data regularly, explore our ready-made Dashboard Templates on NextGenTemplates that use SQL-powered data for business reporting.
📺 Visit our YouTube channel @NeoTechNavigators for step-by-step video tutorials and dashboard demos on this topic.
🧪 Try It Yourself
Write a query that shows the 2 lowest-paid employees (name and salary, sorted by salary ascending).
SELECT Name, Salary
FROM Employees
ORDER BY Salary ASC
LIMIT 2;
Expected Output:
+-----------+--------+ | Name | Salary | +-----------+--------+ | Emma W. | 51000 | | Priya K. | 55000 | +-----------+--------+
📝 What You Learned in This Chapter
LIMIT(MySQL/PostgreSQL) restricts the number of rows returnedTOP(SQL Server) achieves the same result with different syntaxFETCH FIRST(Oracle/Standard SQL) is the ANSI standard approachOFFSETskips rows — combined with LIMIT for pagination- Always use ORDER BY with LIMIT for predictable results
Frequently Asked Questions
LIMIT is used in MySQL, PostgreSQL, and SQLite and goes at the end of the query. TOP is used in SQL Server and goes right after SELECT. The result is identical.OFFSET skips a specified number of rows before the database starts returning results. It is commonly used with LIMIT for pagination. LIMIT 10 OFFSET 20 skips the first 20 rows and returns rows 21 through 30.LIMIT page_size OFFSET (page_number - 1) * page_size. For 10 rows per page: Page 1 = LIMIT 10 OFFSET 0, Page 2 = LIMIT 10 OFFSET 10, Page 3 = LIMIT 10 OFFSET 20.📖 Chapter 12 of 30 — SQL Tutorial on Neotech Navigators



