LIMIT and OFFSET in SQL
LIMIT restricts the number of rows a query returns. OFFSET skips a specified number of rows before returning the rest. Both operate on the final sorted result set.
Before this ORDER BY and Result Sorting
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.
To get the three cheapest products:
SELECT id, name, price
FROM products
ORDER BY price
LIMIT 3SQL sorts all products by price, then stops after the first three. The rest of the rows are never returned.
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:
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.
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.
You want the 5 most recently hired employees. Which query gives you a reliable answer?
9 LIMIT and OFFSET practice problems
Write a query to return the ID, name, and email of the 5 most recently registered customers.
Write a query to return the ID, name, and price of the 3 cheapest products.
Write a query to return the ID, name, and hire date of the 4 most recently hired employees.
Write a query to return the ID, name, and email for the 10 customers on that page.
Write a query to return the ID, name, and price of those 5 products.
Write a query to return the ID, name, and email of the accounts on that page.
Write a query to return the ID and name of every product after the first 5.
Write a query to return the ID, name, and price for every product in that batch.
Write a query to return the ID, name, and price for every product in that batch.
These problems are part of the LIMIT and OFFSET lesson in SQLMaxx, with instant grading and a worked solution on each.
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.
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.
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.
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.
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.
Practice, feedback, mastery, review. That's the loop that turns reading into real skill.
Start freeNo account, no credit card. Start solving in under a minute.