đ 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
| Database System | Clause |
|---|---|
| MySQL | LIMIT |
| PostgreSQL | LIMIT |
| SQLite | LIMIT |
| Microsoft SQL Server | TOP |
| Oracle (12c+) | FETCH FIRST |
đ Sample Table
Consider the following Students table:
| StudentID | Name | Department | Marks |
|---|---|---|---|
| 101 | Alice | Computer Science | 92 |
| 102 | Bob | Mathematics | 85 |
| 103 | Charlie | Physics | 88 |
| 104 | David | Computer Science | 95 |
| 105 | Eva | Mathematics | 90 |
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
| Feature | LIMIT | TOP | FETCH FIRST |
|---|---|---|---|
| Supported By | MySQL, PostgreSQL, SQLite | SQL Server | Oracle (12c+), SQL Standard |
| Limits Rows | â | â | â |
| Supports ORDER BY | â | â | â |
| Supports Pagination | With OFFSET | Typically with OFFSET ... FETCH | With 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
đ 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.