SQL Select Top
A row limit is useful for a preview or a ranked list. The spelling differs across database systems: SQLite and MySQL use LIMIT, SQL Server supports TOP, and several systems support FETCH FIRST.
Understand the idea
Choose an order before applying a limit. Otherwise the database can return any qualifying rows. This example asks for the two most expensive products. A unique final sort key makes the selection stable if prices tie. For a large paginated list, OFFSET can become expensive; the keyset pagination lesson shows another approach.
Where to try it
Run this example in the SQL playground. The lesson link loads the shop tables and sample rows before this query. Select Run when you are ready. Every run starts fresh, so your changes do not affect the website or a previous run.
SQLite sample data · MySQL / MariaDB sample data · See the sample tables
A worked example
SELECT name, price_cents
FROM products
ORDER BY price_cents DESC, product_id
LIMIT 2;
Read the result
Desk lamp at 1800 cents and Canvas bag at 900 cents are returned. SQL Server's equivalent starts SELECT TOP (2) name, price_cents and keeps the same ORDER BY.
Try one change
Return the two lowest-priced products. Then add an OFFSET of 1 and explain which row is skipped.