SyntaxStudy
Sign Up
Home MySQL Reference ORDER BY / LIMIT / OFFSET

ORDER BY / LIMIT / OFFSET

command

ORDER BY sorts results; LIMIT restricts the number of rows; OFFSET skips rows for pagination.

Syntax

ORDER BY col ASC|DESC LIMIT n OFFSET m

Example

sql
-- Sort descending, get top 5
SELECT * FROM products
ORDER BY price DESC
LIMIT 5;

-- Pagination (page 3, 10 per page)
SELECT * FROM posts
ORDER BY created_at DESC
LIMIT 10 OFFSET 20;