← Back to SQL, page 1

sql-0016 · Medium mid

LIMIT, FETCH FIRST and TOP all restrict how many rows come back. Which engines support which, and what is the standard form?

The SQL standard form is OFFSET n ROWS FETCH FIRST m ROWS ONLY, supported by PostgreSQL, Oracle and SQL Server, while PostgreSQL, MySQL and SQLite use LIMIT m OFFSET n. SQL Server also has SELECT TOP m, and SQLite does not accept FETCH FIRST. In every form, ORDER BY is what makes the chosen rows meaningful.

Full answer

Every engine can return "the first few rows", but the spelling differs.

  • LIMIT 3 OFFSET 2: PostgreSQL, MySQL, SQLite.
  • OFFSET 2 ROWS FETCH FIRST 3 ROWS ONLY: the SQL standard; PostgreSQL, Oracle (12c and later), SQL Server (2012 and later, which needs ORDER BY).
  • SELECT TOP 3 ...: SQL Server.

PostgreSQL accepts both of its forms:

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');
sql
SELECT name, salary FROM employees
ORDER BY salary DESC NULLS LAST, name
OFFSET 1 ROWS FETCH FIRST 2 ROWS ONLY;
namesalary
Mei95000
Ravi95000

SQLite accepts LIMIT but not the standard syntax:

sql
SELECT name, salary FROM employees
ORDER BY salary DESC NULLS LAST, name
LIMIT 2 OFFSET 1;
namesalary
Mei95000
Ravi95000
sql
SELECT name FROM employees ORDER BY salary DESC FETCH FIRST 2 ROWS ONLY;  -- error on purpose
text
Error: near "FETCH": syntax error

The standard form has one feature LIMIT lacks: FETCH FIRST 2 ROWS WITH TIES also returns any rows that tie with the last one on the ORDER BY key. PostgreSQL supports it since version 13:

sql
SELECT name, salary FROM employees
ORDER BY salary DESC NULLS LAST
FETCH FIRST 2 ROWS WITH TIES;
namesalary
Asha120000
Ravi95000
Mei95000

Ravi and Mei both earn 95000 and share second place, so WITH TIES returns three rows for "top 2". A plain LIMIT 2 would have dropped one of them, and which one would depend on the plan.

The TOP and Oracle details come from vendor documentation and were not run here. Whatever the syntax, without ORDER BY the database is free to pick any rows.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

SELECT name, salary FROM employees
ORDER BY salary DESC NULLS LAST, name
LIMIT 2 OFFSET 1;

-- error on purpose
SELECT name FROM employees ORDER BY salary DESC FETCH FIRST 2 ROWS ONLY;

Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.

Key points

  • Standard: OFFSET ... FETCH FIRST n ROWS ONLY
  • LIMIT/OFFSET: PostgreSQL, MySQL, SQLite
  • TOP: SQL Server
  • WITH TIES returns extra rows that tie with the last row

Common mistakes

  • Using LIMIT without ORDER BY and expecting the same rows each time
  • Assuming SQLite accepts FETCH FIRST because PostgreSQL does

Follow-ups

  • How would you get top 3 salaries including ties on SQLite?
  • Why does OFFSET get slow on large tables?

Tips

  • Mention WITH TIES; it shows you know the standard goes beyond LIMIT