0PricingLogin
SQL Academy · Lesson

LIMIT and OFFSET

Fetch the first N rows and paginate.

Fetching Just a Few Rows

Sometimes you don't want every matching row, just the first handful. LIMIT caps how many rows the query returns.

This returns at most 5 products.

SELECT name, price
FROM products
LIMIT 5;

LIMIT Without ORDER BY Is Random

LIMIT on its own gives you some rows, but which rows is undefined. Always pair it with ORDER BY so the result is meaningful and repeatable.

-- The 5 newest orders, deterministically
SELECT id, total, created_at
FROM orders
ORDER BY created_at DESC
LIMIT 5;

All lessons in this course

  1. Sorting with ORDER BY
  2. Sorting by Multiple Columns
  3. LIMIT and OFFSET
  4. Finding Top-N Records
← Back to SQL Academy