01 / 21

LIMIT / TOP
Restricting Result Size

Learn how to control how many rows your SQL queries return using dialect-specific keywords like LIMIT, TOP, and FETCH FIRST.

SQL

What Does Restricting Results Mean?

When you run a SELECT query, the database may return thousands or even millions of rows. Often, you only need a small subset: the first 10 products, the top 5 scorers, or just a quick preview of the data.

Restricting result size means telling the database: "give me only \( n \) rows from the full result set." This is essential for performance, pagination, and readability.

Why Limit Your Results?

★ Performance
Returning fewer rows means less data transferred, faster queries, and lower memory usage.
★ Pagination
Display results page-by-page (rows 1-10, then 11-20, etc.) in web apps and reports.
★ Data Preview
Quickly inspect a table's structure and sample data without loading the entire dataset.

The Big Picture: Query Flow

Full Table
\(n\) rows
WHERE Filtered LIMIT Result 3 rows only LIMIT / TOP clips the output after filtering and sorting Execution order: FROM → WHERE → ORDER BY → LIMIT LIMIT is applied last, after all other clauses

Core Syntax: LIMIT

Used in MySQL, PostgreSQL, SQLite, and MariaDB. The LIMIT clause goes at the very end of your query.

SELECT column1, column2 FROM table_name WHERE condition ORDER BY column1 LIMIT 10;

Core Syntax: TOP

Used in Microsoft SQL Server and MS Access. The TOP keyword goes immediately after SELECT, not at the end.

SELECT TOP 10 column1, column2 FROM table_name WHERE condition ORDER BY column1;

Core Syntax: FETCH FIRST

The ANSI SQL:2008 standard introduced FETCH FIRST. This is used in Oracle 12c+, DB2, and PostgreSQL (which supports both LIMIT and FETCH).

SELECT column1, column2 FROM table_name ORDER BY column1 OFFSET 0 ROWS FETCH FIRST 10 ROWS ONLY;

LIMIT vs. TOP vs. FETCH

Feature LIMIT TOP FETCH FIRST
Databases MySQL, PostgreSQL, SQLite SQL Server, MS Access Oracle 12c+, DB2, PostgreSQL
Position End of query After SELECT End of query
OFFSET support LIMIT \(n\) OFFSET \(m\) No built-in offset OFFSET \(m\) ROWS
ANSI standard No No Yes

OFFSET: Skipping Rows

OFFSET lets you skip a specified number of rows before returning results. Combined with LIMIT, this enables pagination.

-- Page 1: rows 1-5 SELECT * FROM products ORDER BY price DESC LIMIT 5 OFFSET 0; -- Page 2: rows 6-10 SELECT * FROM products ORDER BY price DESC LIMIT 5 OFFSET 5;

How to Apply LIMIT/TOP

1
Identify your database dialect. Are you using MySQL, SQL Server, Oracle, or another system?
2
Write your base query with SELECT, FROM, WHERE, and ORDER BY as needed.
3
Choose the correct keyword: LIMIT (MySQL/PostgreSQL), TOP (SQL Server), or FETCH FIRST (Oracle/ANSI).
4
Place it correctly: TOP goes after SELECT; LIMIT and FETCH FIRST go at the end.
5
Add OFFSET if paginating. Remember: always pair OFFSET with ORDER BY for predictable results.

Example 1: Simple LIMIT

Get the 5 most expensive products from a products table.

SELECT name, price FROM products ORDER BY price DESC LIMIT 5;

Example 2: TOP with PERCENT

SQL Server's TOP has a unique feature: you can select a percentage of rows instead of a fixed count.

-- Top 10% of highest-paid employees SELECT TOP 10 PERCENT name, salary FROM employees ORDER BY salary DESC;

Example 3: Pagination in Action

Display page 3 of a product listing, with 10 items per page, across three dialects:

MySQL / PostgreSQL
SELECT * FROM products
ORDER BY id
LIMIT 10 OFFSET 20;
Oracle 12c+
SELECT * FROM products
ORDER BY id
OFFSET 20 ROWS
FETCH FIRST 10 ROWS ONLY
;

Why ORDER BY Matters

Without ORDER BY, the database returns rows in an arbitrary order. Using LIMIT without ORDER BY means you get random, unpredictable rows that may change between executions.

Without ORDER BY Row 47: Banana Row 3: Apple Row 91: Cherry LIMIT 3 Random order! Different each run With ORDER BY name Row 3: Apple Row 47: Banana Row 91: Cherry LIMIT 3 Deterministic order Same result every time

Common Mistakes

✕ Wrong Placement
Writing SELECT LIMIT 5 * instead of putting LIMIT at the end. Or putting TOP at the end in SQL Server.
✕ Missing ORDER BY
Using LIMIT without ORDER BY and expecting consistent, repeatable results. The order is not guaranteed.
✕ Mixing Dialects
Using LIMIT on SQL Server or TOP on MySQL. Each database has its own syntax for row restriction.

Summary Cheat Sheet

Goal MySQL / PostgreSQL SQL Server Oracle 12c+
First 10 rows LIMIT 10 SELECT TOP 10 FETCH FIRST 10 ROWS ONLY
Skip 20, take 10 LIMIT 10 OFFSET 20 Use OFFSET...FETCH* OFFSET 20 ROWS FETCH FIRST 10 ROWS ONLY
Top 10% Not supported SELECT TOP 10 PERCENT FETCH FIRST 10 PERCENT ROWS ONLY

Question 1 of 5

In MySQL, which clause correctly returns only the first 15 rows?

Question 2 of 5

Where does the TOP keyword appear in a SQL Server query?

Question 3 of 5

You want page 4 of results with 20 items per page using MySQL. What should the OFFSET value be?

Question 4 of 5

What is the main risk of using LIMIT without an ORDER BY clause?

Question 5 of 5

Which row-limiting syntax follows the ANSI SQL standard and works in Oracle 12c+?