Tier 1 · Foundations

LIMIT and OFFSET in SQL

By Owen Middleton · Updated September 2026 · Examples run on PostgreSQL 17

What are LIMIT and OFFSET in SQL?

LIMIT caps how many rows a query returns.

By itself that sounds simple. The power shows up when you pair it with ORDER BY. Order your rows by something meaningful — price, date, score — and LIMIT gives you the actual top or bottom of your dataset. Not a random sample, not an arbitrary set of rows: the specific ones that rank highest or lowest by the metric you care about.

This is an everyday analyst task. Which five customers joined most recently? What are the three most expensive products? Who are the top ten earners this quarter? All of these are just ORDER BY plus LIMIT. Write the sort that defines "top" or "most recent" or "most expensive," then cap the result at the number you need.

How do you return the top N rows in SQL?

To get the three cheapest products:

SELECT id, name, price
FROM products
ORDER BY price
LIMIT 3

SQL sorts all products by price, then stops after the first three. The rest of the rows are never returned.

How do you paginate results with LIMIT and OFFSET?

OFFSET pairs with LIMIT for pagination. It skips a number of rows before the count starts. To get products ranked 6th through 10th by price:

SELECT id, name, price
FROM products
ORDER BY price
LIMIT 5 OFFSET 5

OFFSET 5 skips the five cheapest. LIMIT 5 returns the next five. Page 1 of a paginated list is OFFSET 0, page 2 is OFFSET 10, page 3 is OFFSET 20.

OFFSET without LIMIT is valid — it skips N rows and returns everything after. LIMIT without OFFSET returns the first N rows.

Why does LIMIT return different rows each run?

The one thing that trips people up: LIMIT without ORDER BY gives you unpredictable results.

Two runs of the same query can return different rows. SQL isn't broken — it's being honest about the fact that without a sort, "which rows come first" has no meaningful answer. Any time LIMIT is doing real work, ORDER BY needs to be there too.

Check your understanding

You want the 5 most recently hired employees. Which query gives you a reliable answer?

Practice LIMIT and OFFSET in SQL

Practice · easy ecommerce · Brightlane

Brightlane's customer success team is onboarding a new account manager who wants to review the most recent sign-ups first.

Write a query to return the ID, name, and email of the 5 most recently registered customers.

Assumptions:

  • The customers table contains every customer Brightlane has on file.
  • The created_at column records when each customer registered.

Output:

  • One row per customer, with columns id, name, and email, sorted by created_at descending and capped at 5 rows.
Schema · ecommerce5 tables? = nullable
categories
idinteger
nametext
parent_id?integer
products
idinteger
nametext
category_id?integer
pricenumeric
stock_qtyinteger
attributes?jsonb
order_items
idinteger
order_id?integer
product_id?integer
quantityinteger
unit_pricenumeric
customers
idinteger
nametext
emailtext
city?text
countrytext
created_attimestamptz
is_activeboolean
orders
idinteger
customer_id?integer
ordered_attimestamptz
statustext
total_amountnumeric

Run previews · Check grades

Write a query, then run it to see results here.

Worked solution

The full breakdown walks through the shape, each clause, why this approach beats the alternatives, and the trap to avoid.

See the full worked solution

9 LIMIT and OFFSET practice problems

Start learning to practice all 9 LIMIT and OFFSET problems, with instant grading and mastery tracking.

Common questions about LIMIT and OFFSET

What happens if LIMIT is larger than the number of rows?

You get every row and no error. LIMIT sets a ceiling, not a target, so asking for a hundred thousand rows from a table holding sixty-three returns the sixty-three. Nothing is padded and nothing complains.

Can you use OFFSET without LIMIT?

Yes. OFFSET on its own skips the rows you name and returns everything after them, which is how you ask for the tail of a result. LIMIT on its own is the more common half of the pair.

Is LIMIT 0 valid SQL?

Yes. It returns no rows while still running the query, which makes it a cheap way to check that a statement parses and that the column names and types are what you expected before you run it for real.

Does LIMIT change which rows get sorted?

No. ORDER BY applies to the whole result and LIMIT then cuts the front off it, which is why the pair gives you a genuine top ten rather than ten arbitrary rows sorted after the fact.

How you actually get good at SQL

Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.

That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.

A stack of SQL practice problem cards, the top card showing an employees table.
615 problems · 66 concepts

Real problems. Not toy examples.

615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.

A retro computer showing a SQL query marked correct with a green checkmark.
Instant AI feedback

Write a query. Know if it's right in one second.

No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.

A circular mastery progress dial filling from blue to green, the SQLMaxx diamond at its center.
Mastery tracking

Stop guessing whether you actually know it.

SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.

A SQL query editor circled by a blue return arrow with a clock, scheduled to come back for review.
Spaced review

Learn it once. Keep it for good.

Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.