SQL TOP, LIMIT and FETCH FIRST
SQL

SQL TOP, LIMIT and FETCH FIRST — How to Limit Rows Returned

CHAPTER 12 of 30 SQL Tutorial — Free Course on Neotech Navigators
When tables have thousands or millions of rows, you rarely want all of them at once. The SQL TOP, LIMIT and FETCH FIRST clauses let you limit the number of rows returned by a query. Different databases use different syntax. In this chapter, you will learn all three approaches with practical examples.

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:

TABLE: Employees
+----+-----------+------------+--------+-----------+
| 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:

SQL — MySQL / PostgreSQL
SELECT Name, Salary
FROM Employees
ORDER BY Salary DESC
LIMIT 3;
✓ OUTPUT — Top 3 Salaries
+-----------+--------+
| 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:

SQL — SQL Server
SELECT TOP 3 Name, Salary
FROM Employees
ORDER BY Salary DESC;

Oracle / Standard SQL — FETCH FIRST

Oracle and the SQL standard use FETCH FIRST:

SQL — Oracle / Standard
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:

SQL — Skip 2, Return 3
SELECT Name, Salary
FROM Employees
ORDER BY ID
LIMIT 3 OFFSET 2;
✓ OUTPUT — Rows 3, 4, 5
+-----------+--------+
| 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:

SQL
SELECT Name, Department, Salary
FROM Employees
WHERE Salary > 55000
ORDER BY Salary DESC
LIMIT 2;
✓ OUTPUT
+-----------+------------+--------+
| 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).

ANSWER
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 returned
  • TOP (SQL Server) achieves the same result with different syntax
  • FETCH FIRST (Oracle/Standard SQL) is the ANSI standard approach
  • OFFSET skips rows — combined with LIMIT for pagination
  • Always use ORDER BY with LIMIT for predictable results

Frequently Asked Questions

What is the difference between LIMIT and TOP in SQL?
They do the same thing — restrict how many rows are returned. 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.
What does OFFSET do in SQL?
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.
Can I use LIMIT without ORDER BY?
Technically yes, but the results will be unpredictable. Without ORDER BY, the database returns rows in whatever internal order it chooses, which can change between executions. Always use ORDER BY with LIMIT for consistent results.
How do I implement pagination with LIMIT?
Use the formula 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.
Does LIMIT improve query performance?
LIMIT can improve performance because the database stops processing once the required number of rows is found. However, if combined with ORDER BY on an unindexed column, the database may still need to scan and sort the entire table first before applying LIMIT.

📖 Chapter 12 of 30 — SQL Tutorial on Neotech Navigators

PK
Meet PK, the founder of NeotechNavigators.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your data analysis skills to the next level!
https://neotechnavigators.com