SyntaxStudy
Sign Up
MySQL Beginner 7 min read

ORDER BY and LIMIT

ORDER BY and LIMIT

ORDER BY

SELECT * FROM products ORDER BY price;           -- ASC default
SELECT * FROM products ORDER BY price DESC;
SELECT * FROM users ORDER BY last_name, first_name;
SELECT * FROM orders ORDER BY created_at DESC;

LIMIT and OFFSET

SELECT * FROM products LIMIT 10;
SELECT * FROM products LIMIT 10 OFFSET 20;  -- page 3

-- Equivalent shorthand
SELECT * FROM products LIMIT 20, 10;

Pagination Pattern

-- Page 1: LIMIT 10 OFFSET 0
-- Page 2: LIMIT 10 OFFSET 10
-- Page n: LIMIT 10 OFFSET (n-1)*10
Pro Tip

Always use ORDER BY with LIMIT to get predictable, consistent results.