Module 01 · lesson 7
LIMIT and OFFSET: limiting the count
The site's home page needs a "TOP 3 most expensive products" block. Not all 14 — exactly three.
You'll need:ORDER BY: sorting the result(opens in a new tab)
Limiting the count
LIMIT returns no more than the number of rows you specify. For example, the 5 products with the largest stock:
SELECT name, stock
FROM products
ORDER BY stock DESC
LIMIT 5;
Skipping the start: OFFSET
OFFSET skips the first N rows. Together with LIMIT it's the basis of pagination (page-by-page output) — the second "page" of 5 products:
ORDER BY stock DESC
LIMIT 5 OFFSET 5 -- skip the first 5, take the next 5
Important: LIMIT without ORDER BY gives an unpredictable set — order first, then limit.
Your turn
The home page needs a "TOP 3 most expensive" block. Output the name and price of the three most expensive products.
Summary
LIMIT N— no more than N rows.OFFSET M— skip the first M.LIMITandOFFSETalways go together withORDER BY, otherwise the order is random.