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:
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
OFFSET 1 ROWS FETCH FIRST 2 ROWS ONLY;| name | salary |
|---|---|
| Mei | 95000 |
| Ravi | 95000 |
SQLite accepts LIMIT but not the standard syntax:
SELECT name, salary FROM employees
ORDER BY salary DESC NULLS LAST, name
LIMIT 2 OFFSET 1;| name | salary |
|---|---|
| Mei | 95000 |
| Ravi | 95000 |
SELECT name FROM employees ORDER BY salary DESC FETCH FIRST 2 ROWS ONLY; -- error on purposeError: near "FETCH": syntax errorThe 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:
SELECT name, salary FROM employees
ORDER BY salary DESC NULLS LAST
FETCH FIRST 2 ROWS WITH TIES;| name | salary |
|---|---|
| Asha | 120000 |
| Ravi | 95000 |
| Mei | 95000 |
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.
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