Sub topic 14
TOP, FETCH FIRST, and Other Dialect Syntax
This chapter assumes that you know basic SELECT, FROM, WHERE, and ORDER BY syntax. You should also understand why a stable ORDER BY is important when returning only part of a result.
SQL has standard ways to limit and skip rows, but database systems also provide different syntax. The goal is the same: return a controlled slice of an ordered result.
The main syntax families
| Database system | Return the first rows | Skip and return rows |
|---|---|---|
| PostgreSQL, MySQL, SQLite | `LIMIT 10` | `LIMIT 10 OFFSET 20` |
| SQL Server | `TOP (10)` | `OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY` |
| Oracle, standard SQL | `FETCH FIRST 10 ROWS ONLY` | `OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY` |
These forms are similar, but they are not interchangeable in every database.
`TOP` in SQL Server
SQL Server commonly uses TOP to restrict the number of rows:
SELECT TOP (5) order_id, order_date, total
FROM orders
ORDER BY order_date DESC, order_id DESC;The parentheses are recommended because they make the expression clear. TOP belongs immediately after SELECT.
SQL Server also supports a percentage:
SELECT TOP (10) PERCENT order_id, order_date, total
FROM orders
ORDER BY order_date DESC, order_id DESC;Without ORDER BY, the database can choose any rows. The result is therefore not a reliable “top” five unless you define what top means.
SQL Server can include ties with WITH TIES:
SELECT TOP (5) WITH TIES order_id, order_date, total
FROM orders
ORDER BY order_date DESC;This can return more than five rows when additional rows have the same final sort value. Add a unique tie-breaker when you need exactly five rows.
`FETCH FIRST` and `OFFSET`
FETCH FIRST is part of the standard SQL row-limiting syntax and is supported by Oracle, PostgreSQL, and several other systems:
SELECT order_id, order_date, total
FROM orders
ORDER BY order_date DESC, order_id DESC
FETCH FIRST 5 ROWS ONLY;To skip rows, place OFFSET before FETCH:
SELECT order_id, order_date, total
FROM orders
ORDER BY order_date DESC, order_id DESC
OFFSET 20 ROWS
FETCH NEXT 10 ROWS ONLY;This returns rows 21 through 30 in the defined ordering. FETCH NEXT and FETCH FIRST are equivalent for this purpose.
SQL Server uses the same OFFSET and FETCH structure, but requires ORDER BY with it:
SELECT order_id, order_date, total
FROM orders
ORDER BY order_date DESC, order_id DESC
OFFSET 20 ROWS
FETCH NEXT 10 ROWS ONLY;Translating between dialects
Suppose the original query uses PostgreSQL-style syntax:
SELECT order_id, order_date, total
FROM orders
ORDER BY order_date DESC, order_id DESC
LIMIT 10 OFFSET 20;The equivalent standard or SQL Server form is:
SELECT order_id, order_date, total
FROM orders
ORDER BY order_date DESC, order_id DESC
OFFSET 20 ROWS
FETCH NEXT 10 ROWS ONLY;For only the first five rows in SQL Server, use TOP:
SELECT TOP (5) order_id, order_date, total
FROM orders
ORDER BY order_date DESC, order_id DESC;For MySQL, the alternative comma form puts the offset first:
SELECT order_id, order_date, total
FROM orders
ORDER BY order_date DESC, order_id DESC
LIMIT 20, 10;Prefer LIMIT 10 OFFSET 20 when your application supports it because the meaning is easier to read.
Choosing a portable approach
If your application supports several database systems, keep pagination logic separate from query construction. Generate the correct limiting syntax for the active database adapter. In every dialect:
- Add
ORDER BYbefore limiting rows. - Include a unique tie-breaker, such as
order_id. - Treat the limit and offset as parameters when the driver supports parameterized values.
- Consider keyset pagination when offsets become large.
Exercise
Translate SELECT ... ORDER BY order_date DESC, order_id DESC LIMIT 10 OFFSET 20 into SQL Server syntax.
Show answer
Use ORDER BY order_date DESC, order_id DESC OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY.