Hook: LIMIT is the restaurant host for your result set: it decides how many rows actually get served, which is why interviewers use it to test whether you understand ordering and pagination, not just syntax.
Question: What does LIMIT do in SQL?
Answer: LIMIT caps the number of rows returned by a query. It is usually paired with ORDER BY so the subset is predictable, and with OFFSET when you want paging. Without ORDER BY, LIMIT still works, but the rows you get are not guaranteed to be the same each time.
Interview-Ready Answer: "I use LIMIT to cap the number of rows a query returns. If I want a stable result, I always add ORDER BY, because LIMIT alone does not define which rows are chosen. For paging, I combine it with OFFSET, and I also remember that LIMIT is dialect-specific—standard SQL often uses FETCH FIRST instead."
Detailed Explanation: LIMIT is a clause that tells the database the maximum number of rows to return. Think of it as a row cap, not a row filter: it does not decide which rows qualify, only how many of the qualifying rows you see.
WHERE, grouping, and sorting.FROM/JOIN → WHERE → GROUP BY → HAVING → ORDER BY → LIMIT/OFFSET.ORDER BY step matters because LIMIT takes the first N rows of the ordered result. If there is no order, the database may return any matching rows.OFFSET is present, the database skips the first M rows and then returns the next N rows. That makes deep pages slower because the skipped rows still have to be found or counted.| Clause | What it does | When it acts | Good to know |
|---|---|---|---|
| LIMIT | Caps rows | After sort | Dialect-specific |
| WHERE | Filters rows | Before sort | Changes which rows qualify |
| ORDER BY | Sorts rows | Before LIMIT | Needed for stable paging |
| FETCH FIRST | Standard cap | After sort | SQL:2008+ syntax |
| TOP | Caps rows | Query start | Common in SQL Server |
When and why to use it: use LIMIT for previews, dashboards, search results, API responses, and any top-N report. It is also useful in debugging when you want a tiny sample, and LIMIT 0 is a neat trick when you want the column list without any data.
Performance and complexity: if the engine must sort many matching rows, the cost can be roughly O(R log R) time and O(R) memory for R candidate rows. If an index already provides the needed order, the engine may read only the first N rows, which feels close to O(N). Deep pagination is the classic trap: asking for page 1000 with page size 20 means skipping 19,980 rows, which can become noticeably slow.
LIMIT 0 is valid and returns no rows.LIMIT without ORDER BY is legal but not deterministic.LIMIT; SQL Server usually uses TOP or OFFSET ... FETCH; standard SQL commonly uses FETCH FIRST n ROWS ONLY.Real-World Example: In an e-commerce product listing service, the API might return 20 items per page with ORDER BY created_at DESC, product_id DESC LIMIT 20 OFFSET 40. That gives the frontend page 3 of a catalog, and the extra product ID in the order clause keeps the page stable when two products share the same timestamp.
What goes wrong when LIMIT is misunderstood? A team forgets ORDER BY, ships a “top products” endpoint, and users start seeing duplicates on page 2 and missing items on page 3. The symptom is subtle: the same request parameters return different product IDs across deployments or even across repeated requests, support tickets mention “items moved around,” and logs show the query was fast but inconsistent. The bug is not LIMIT itself; it is using LIMIT without a deterministic order.
-- LIMIT demo: predictable top-N, pagination, and edge cases
-- This script is intentionally simple and works in SQLite, PostgreSQL, and MySQL 8+.
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer TEXT NOT NULL,
status TEXT NOT NULL,
created_at TEXT NOT NULL,
total_cents INTEGER NOT NULL
);
INSERT INTO orders (order_id, customer, status, created_at, total_cents) VALUES
(1, 'Ava', 'PAID', '2026-07-01 09:15:00', 2599),
(2, 'Ben', 'PAID', '2026-07-01 09:20:00', 1899),
(3, 'Cara', 'CANCELLED', '2026-07-01 09:25:00', 4999),
(4, 'Dan', 'PAID', '2026-07-01 09:30:00', 1299),
(5, 'Eli', 'PAID', '2026-07-01 09:35:00', 3999);
-- Best practice: LIMIT + ORDER BY gives a predictable top-N result.
SELECT order_id, customer, total_cents
FROM orders
WHERE status = 'PAID'
ORDER BY created_at DESC, order_id DESC
LIMIT 3;
-- Pagination: skip the first 2 matching rows, then return the next 2.
SELECT order_id, customer, total_cents
FROM orders
WHERE status = 'PAID'
ORDER BY created_at DESC, order_id DESC
LIMIT 2 OFFSET 2;
-- Edge case: LIMIT 0 is valid and returns no rows.
-- Useful when you only want to validate the shape of a query.
SELECT order_id, customer
FROM orders
WHERE status = 'PAID'
ORDER BY created_at DESC
LIMIT 0;
-- Gotcha demo: this is legal, but the chosen rows are not guaranteed.
-- Without ORDER BY, the database may pick any 2 matching rows.
SELECT order_id, customer
FROM orders
WHERE status = 'PAID'
LIMIT 2;
-- Another edge case: OFFSET beyond the result size returns an empty set.
SELECT order_id, customer
FROM orders
WHERE status = 'PAID'
ORDER BY created_at DESC, order_id DESC
LIMIT 2 OFFSET 10;Follow-up & Tricky Questions:
FETCH FIRST n ROWS ONLY. SQL Server usually uses TOP or OFFSET ... FETCH.created_at DESC, id DESC. This prevents rows with equal timestamps from jumping between pages.Common Mistakes:
Memory Hook: “LIMIT is the bouncer; ORDER BY is the guest list.” LIMIT decides how many people get in, but ORDER BY decides who stands at the front of the line.
Cheat Sheet:
Practice Tasks: