SQL TOP, LIMIT, FETCH FIRST, and ROWNUM Explained
For SQL beginners who want only the first rows of a result, or one page of it, in SQL Server, MySQL, PostgreSQL, Oracle or SQLite.
On this page
TOP, LIMIT, FETCH FIRST and ROWNUM keep only the first few rows that a query returns. For example, you can return the five newest orders from an orders table, or one page of twenty. The keyword depends on the database engine.
Find your engine in the -- comment lines:
-- MySQL, MariaDB, PostgreSQL, SQLite
SELECT columns FROM table_name
ORDER BY column
LIMIT n OFFSET m
-- SQL Server
SELECT TOP (n) columns FROM table_name
ORDER BY column
-- SQL standard: PostgreSQL, Oracle 12c and later, SQL Server 2012 and later, Db2, MariaDB 10.6 and later
SELECT columns FROM table_name
ORDER BY column
OFFSET m ROWS FETCH FIRST n ROWS ONLY
-- Oracle 11g and earlier
SELECT * FROM (
SELECT columns FROM table_name ORDER BY column
)
WHERE ROWNUM <= n
ORDER BYsorts the rows. LIMIT, TOP, FETCH FIRST or ROWNUM then keeps the first rows in that order. Without ORDER BY, the engine decides which rows come first.nis the number of rows to keep.OFFSET mskips the first m rows of the sorted result. OFFSET is optional, but SQL Server needs it before FETCH.ROWNUMnumbers the rows in the order they reach Oracle. The inner SELECT, called a subquery, sorts the rows first, so they arrive sorted.
Create a sample table of six students and fill it:
CREATE TABLE students (
student_id int PRIMARY KEY,
name varchar(20),
age int
);
INSERT INTO students VALUES
(1, 'John', 20),
(2, 'Jane', 22),
(3, 'Sam', 19),
(4, 'Mike', 21),
(5, 'Emily', 23),
(6, 'Lena', 21);
Mike and Lena are both 21. Two rows with the same value in the sorted column are a tie.
The result tables below come from runs on PostgreSQL 18.6, MySQL 9.1, MariaDB 12.3 and SQLite 3.50. SQL Server and Oracle were not run. For those two engines, the expected rows come from the vendor's documentation.
The database applies the clauses in this order: ORDER BY sorts the rows, OFFSET skips the first m rows, and LIMIT keeps the next n rows:
LIMIT
LIMIT n keeps the first n rows of the sorted result. For example, you can return the two youngest students.
SELECT student_id, name, age
FROM students
ORDER BY age
LIMIT 2;
| student_id | name | age |
|---|---|---|
| 3 | Sam | 19 |
| 1 | John | 20 |
ORDER BY age put Sam (19) and John (20) first, and LIMIT 2 kept those two. MySQL, MariaDB, PostgreSQL and SQLite take LIMIT as the last clause of the SELECT. A WHERE clause removes rows before the sort, so LIMIT counts only the matching rows.
TOP
TOP (n) keeps the first n rows in SQL Server. For example, TOP (2) returns the two youngest students.
SELECT TOP (2) student_id, name, age
FROM students
ORDER BY age;
According to the TOP documentation, the query returns the same two rows as LIMIT 2: Sam and John. TOP goes right after SELECT, and LIMIT goes at the end.
In a SELECT, the parentheses are optional when n is a plain number. Microsoft recommends writing them every time.
FETCH FIRST
FETCH FIRST n ROWS ONLY keeps the first n rows, in the form of the official SQL:2008 standard. For example, you can return the two youngest students on PostgreSQL.
SELECT student_id, name, age
FROM students
ORDER BY age
FETCH FIRST 2 ROWS ONLY;
| student_id | name | age |
|---|---|---|
| 3 | Sam | 19 |
| 1 | John | 20 |
PostgreSQL and MariaDB returned Sam and John, as LIMIT 2 did. PostgreSQL, Oracle 12c and later, Db2 and MariaDB 10.6 and later take FETCH FIRST. MySQL 9.1 and SQLite 3.50 reject the query with a syntax error near FETCH.
SQL Server 2012 and later take FETCH only after an OFFSET: OFFSET 0 ROWS FETCH NEXT 2 ROWS ONLY. NEXT means the same as FIRST. On SQL Server, TOP and FETCH can't share one query (ORDER BY documentation).
OFFSET
OFFSET m skips the first m rows of the sorted result. For example, with LIMIT you can return the second page of students, two to a page.
SELECT student_id, name, age
FROM students
ORDER BY student_id
LIMIT 2 OFFSET 2;
| student_id | name | age |
|---|---|---|
| 3 | Sam | 19 |
| 4 | Mike | 21 |
OFFSET 2 skipped John and Jane, and LIMIT 2 returned the next two rows, Sam and Mike. At 20 rows a page, page 3 is LIMIT 20 OFFSET 40. A large OFFSET is slow on a big table, because the database still computes every skipped row.
The standard form of the same page is OFFSET 2 ROWS FETCH NEXT 2 ROWS ONLY. MySQL, MariaDB and SQLite also take LIMIT 2, 2, with the offset first. PostgreSQL rejects LIMIT 2, 2.
Practice this in Lesson 5 of the SQL tutorial: you run ORDER BY and LIMIT on a shop database in your browser, ties included, and each exercise is checked.
WITH TIES
WITH TIES keeps the first n rows, plus every later row that ties with row n. For example, you can return the three youngest students, plus anyone as old as the third.
SELECT student_id, name, age
FROM students
ORDER BY age
FETCH FIRST 3 ROWS WITH TIES;
| student_id | name | age |
|---|---|---|
| 3 | Sam | 19 |
| 1 | John | 20 |
| 4 | Mike | 21 |
| 6 | Lena | 21 |
The third row is Mike, aged 21. Lena is 21 too, so WITH TIES added her, and the query returned four rows instead of three.
PostgreSQL 13 and later, Oracle 12c and later and MariaDB 10.6 and later take WITH TIES. SQL Server writes SELECT TOP (3) WITH TIES.
PERCENT
PERCENT keeps a percentage of the rows instead of a number of rows. For example, you can return half of the students. Only SQL Server's TOP and Oracle's FETCH FIRST take PERCENT.
-- SQL Server
SELECT TOP (50) PERCENT student_id, name, age
FROM students
ORDER BY student_id;
-- Oracle 12c and later
SELECT student_id, name, age
FROM students
ORDER BY student_id
FETCH FIRST 50 PERCENT ROWS ONLY;
According to the TOP and SELECT documentation, both queries return John, Jane and Sam. SQL Server rounds a fraction of a row up, so TOP (40) PERCENT of six rows returns three.
ROWNUM
ROWNUM numbers the rows in the order that Oracle selects them: 1 for the first row, 2 for the second. For example, on Oracle 11g you keep the two youngest students with a filter on ROWNUM. Oracle added FETCH FIRST only in 12c.
SELECT * FROM (
SELECT student_id, name, age
FROM students
ORDER BY age
)
WHERE ROWNUM <= 2;
The subquery sorts the students by age. Oracle numbers the sorted rows, and the WHERE keeps numbers 1 and 2: Sam and John. On 12c and later, the ROWNUM documentation says that FETCH FIRST does the job better.
What goes wrong
No ORDER BY
Without ORDER BY, the engine returns the rows that it reaches first. Those rows can change even when the data stays the same. On the newly filled table, PostgreSQL 18 returned John and Jane for the first SELECT below:
SELECT student_id, name, age
FROM students
LIMIT 2;
UPDATE students SET age = 20 WHERE student_id = 1;
SELECT student_id, name, age
FROM students
LIMIT 2;
The UPDATE sets John's age to 20, which is his age already. The second SELECT then returns Jane and Sam:
| student_id | name | age |
|---|---|---|
| 2 | Jane | 22 |
| 3 | Sam | 19 |
PostgreSQL stores an updated row as a new copy. John's new copy comes after Lena's row, so PostgreSQL now reads John last. With an ORDER BY, both runs return the same rows.
A tie at the cut
ORDER BY age gives Mike and Lena, both 21, no fixed order. Ask for three rows, and the third row can be either Mike or Lena:
SELECT student_id, name, age
FROM students
ORDER BY age
LIMIT 3;
| student_id | name | age |
|---|---|---|
| 3 | Sam | 19 |
| 1 | John | 20 |
| 4 | Mike | 21 |
UPDATE students SET age = 21 WHERE student_id = 4 changes no value. After that UPDATE, the same query on PostgreSQL 18 returned Lena in Mike's place.
Add a column with a different value in every row to the ORDER BY, as in ORDER BY age, student_id. Then Mike comes third every time. Pages need that order too, or two pages can disagree about which rows they hold.
ROWNUM before ORDER BY
In one query, Oracle numbers the rows before it sorts them:
SELECT student_id, name, age
FROM students
WHERE ROWNUM <= 2
ORDER BY age;
Oracle keeps the first two rows that it reads, whichever they are, and then sorts only those two. Sort in a subquery, as in the ROWNUM section.
A filter such as WHERE ROWNUM > 1 returns no rows at all. The first row that Oracle reads is always number 1, so that row fails the filter.
UPDATE and DELETE with a row limit
SQL Server's UPDATE TOP (n) and DELETE TOP (n) take no ORDER BY. They change n rows in no particular order (TOP documentation). To delete the two youngest students, pick them in a subquery that sorts by age:
DELETE FROM students
WHERE student_id IN (
SELECT student_id FROM students
ORDER BY age
LIMIT 2
);
PostgreSQL and SQLite deleted Sam and John. SELECT student_id, name, age FROM students ORDER BY student_id now returns:
| student_id | name | age |
|---|---|---|
| 2 | Jane | 22 |
| 4 | Mike | 21 |
| 5 | Emily | 23 |
| 6 | Lena | 21 |
On SQL Server, write SELECT TOP (2) in the subquery. MySQL 9.1 rejects a LIMIT inside IN with "This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'". MariaDB 12.3 gives the same error, with "MariaDB" in place of "MySQL".
On MySQL and MariaDB, a DELETE or an UPDATE takes ORDER BY and LIMIT itself: DELETE FROM students ORDER BY age LIMIT 2. PostgreSQL and SQLite reject that form with a syntax error. SQLite accepts it only when compiled with the option for it.
Run the queries in DbSchema
The SQL Editor in DbSchema runs a query and shows its rows in a grid under the editor. Press Editor in the toolbar, choose SQL Editor, paste a query from this article, and press Run.
If you run a DELETE with Run, the change stays in an open transaction and is not saved yet. Run a SELECT to see which rows remain. Then press Commit to keep the change, or Rollback to undo it.
Download DbSchema at https://dbschema.com/download.html and connect it to PostgreSQL, MySQL, SQL Server, Oracle or SQLite. Paste the CREATE TABLE and INSERT statements into the SQL Editor, then run each query and change n and m. Connecting to a database and the SQL Editor are part of the free Community Edition.
Run these queries against your own database
DbSchema connects to SQL Server, MySQL, PostgreSQL, Oracle, Db2 and SQLite, and its SQL Editor returns the rows so you can check a limit before it ships. The SQL Editor is in the free Community Edition.