LIMIT / TOP / FETCH in SQL

📌 LIMIT, TOP, and FETCH are SQL clauses used to restrict the number of rows returned by a query. They are especially useful when working with large datasets, displaying paginated results, retrieving top-performing records, or improving query performance by fetching only the required rows.

📖 Why Limit Query Results?

By default, the SELECT statement returns all matching rows from a table. In many real-world applications, you only need a subset of the data, such as the first 10 products, the top 5 students, or the latest 20 orders.

Information

Different database systems use different syntax to limit rows:
Database SystemClause
MySQL LIMIT
PostgreSQL LIMIT
SQLite LIMIT
Microsoft SQL Server TOP
Oracle (12c+) FETCH FIRST

📊 Sample Table

Consider the following Students table:

StudentIDNameDepartmentMarks
101AliceComputer Science92
102BobMathematics85
103CharliePhysics88
104DavidComputer Science95
105EvaMathematics90

1ī¸âƒŖ LIMIT (MySQL, PostgreSQL, SQLite)

The LIMIT clause returns only the specified number of rows from the result set.

LIMIT Syntax

SELECT column_name
FROM table_name
LIMIT number_of_rows;

Retrieve First Three Students

SELECT *
FROM Students
LIMIT 3;

This query returns only the first 3 rows from the result set.

LIMIT with ORDER BY

To retrieve the highest or lowest values, combine LIMIT with ORDER BY.

Top Three Students by Marks

SELECT Name,
       Marks
FROM Students
ORDER BY Marks DESC
LIMIT 3;

LIMIT with OFFSET

OFFSET skips a specified number of rows before returning results. It is commonly used for pagination.

LIMIT with OFFSET

SELECT *
FROM Students
LIMIT 2 OFFSET 2;

This query skips the first 2 rows and returns the next 2 rows.

2ī¸âƒŖ TOP (Microsoft SQL Server)

SQL Server uses the TOP keyword to limit the number of rows returned.

TOP Syntax

SELECT TOP number_of_rows column_name
FROM table_name;

Retrieve Top Three Students

SELECT TOP 3 *
FROM Students;

TOP with ORDER BY

Top Three Highest Marks

SELECT TOP 3 Name,
       Marks
FROM Students
ORDER BY Marks DESC;

TOP with Percentage

SQL Server also allows returning a percentage of rows.

TOP Percentage

SELECT TOP 50 PERCENT *
FROM Students;

3ī¸âƒŖ FETCH FIRST (Oracle 12c+ and SQL Standard)

Oracle (12c and later) and several SQL-compliant databases support the FETCH FIRST clause.

FETCH FIRST Syntax

SELECT column_name
FROM table_name
FETCH FIRST number_of_rows ROWS ONLY;

Retrieve First Three Students

SELECT *
FROM Students
FETCH FIRST 3 ROWS ONLY;

FETCH FIRST with ORDER BY

Highest Marks Using FETCH FIRST

SELECT Name,
       Marks
FROM Students
ORDER BY Marks DESC
FETCH FIRST 3 ROWS ONLY;

📊 Comparison of LIMIT, TOP, and FETCH

FeatureLIMITTOPFETCH FIRST
Supported ByMySQL, PostgreSQL, SQLiteSQL ServerOracle (12c+), SQL Standard
Limits Rows✅✅✅
Supports ORDER BY✅✅✅
Supports PaginationWith OFFSETTypically with OFFSET ... FETCHWith OFFSET

📄 Pagination Example

Pagination divides large datasets into smaller pages, making them easier to display in web applications.

Retrieve Page 2 (MySQL/PostgreSQL/SQLite)

SELECT *
FROM Students
ORDER BY StudentID
LIMIT 5 OFFSET 5;

If each page displays 5 records, this query skips the first 5 rows and returns the next 5.

đŸ’ŧ Real-World Example

Suppose a university portal wants to display only the top five students based on their marks.

Top Five Students

SELECT Name,
       Department,
       Marks
FROM Students
ORDER BY Marks DESC
LIMIT 5;

On SQL Server, the equivalent query would use TOP 5, while Oracle would use FETCH FIRST 5 ROWS ONLY.

âš ī¸ Best Practices

Best Practice

Always combine LIMIT, TOP, or FETCH FIRST with ORDER BY when retrieving the "top" or "first" records. Without sorting, the database may return rows in an unspecified order, leading to inconsistent results.

🚀 Key Points to Remember

  • 📌 Use LIMIT in MySQL, PostgreSQL, and SQLite.
  • 📌 Use TOP in Microsoft SQL Server.
  • 📌 Use FETCH FIRST in Oracle 12c+ and SQL-standard databases.
  • 📌 Combine row-limiting clauses with ORDER BY for predictable results.
  • 📌 Use OFFSET for pagination.
  • 📌 Limiting rows can improve performance by reducing the amount of data returned.
>>"Fetching only the data you need makes SQL queries faster, cleaner, and more efficient."

Summary

✅ LIMIT, TOP, and FETCH FIRST are used to restrict the number of rows returned by an SQL query. Although their syntax differs across database systems, they all serve the same purpose: efficiently retrieving a specific subset of data for reporting, analysis, and application development.