SQL LIMIT, TOP and FETCH FIRST
SQL LIMIT restricts how many rows a query returns. It is used for "top 5" lists and for paging through results on a website. Different databases spell it differently, which is a common source of confusion.
LIMIT in SQLite, MySQL and PostgreSQL
Show the three most expensive products. Combine LIMIT with ORDER BY, otherwise "the first three" is not well defined.
SELECT ProductName, Price
FROM Products
ORDER BY Price DESC
LIMIT 3;ProductName | Price -------------+------ Coffee Beans | 22 Olive Oil | 18.75 Almonds | 15 (3 rows)
Pagination with OFFSET
OFFSET skips rows. With 3 rows per page, page 2 starts after skipping 3 rows.
SELECT ProductID, ProductName
FROM Products
ORDER BY ProductID
LIMIT 3 OFFSET 3;ProductID | ProductName ----------+--------------- 4 | Dark Chocolate 5 | Coffee Beans 6 | Almonds (3 rows)
TOP in SQL Server and FETCH FIRST
SQL Server uses TOP after SELECT. Oracle 12c+, PostgreSQL and SQL Server 2012+ also support the standard OFFSET ... FETCH syntax. These do not run in our SQLite playground, so they are shown for reference only.
-- SQL Server
SELECT TOP 3 ProductName, Price FROM Products ORDER BY Price DESC;
-- Standard SQL (Oracle, PostgreSQL, SQL Server)
SELECT ProductName, Price FROM Products
ORDER BY Price DESC
OFFSET 0 ROWS FETCH FIRST 3 ROWS ONLY;Latest Orders Example
A typical "recent activity" widget: the two newest orders.
SELECT OrderID, OrderDate
FROM Orders
ORDER BY OrderDate DESC
LIMIT 2;OrderID | OrderDate --------+----------- 110 | 2026-05-22 109 | 2026-05-07 (2 rows)
Frequently asked questions
What is the SQL Server equivalent of LIMIT?
SELECT TOP n ... or, from SQL Server 2012, ORDER BY ... OFFSET 0 ROWS FETCH NEXT n ROWS ONLY.
Is OFFSET pagination slow on big tables?
Large offsets get slower because the database still reads and skips those rows. For deep paging, keyset pagination (WHERE id > last_seen_id ORDER BY id LIMIT n) is much faster.
Does LIMIT work without ORDER BY?
It works, but the rows you get are not guaranteed to be the same each time. Always pair LIMIT with ORDER BY when the choice of rows matters.