Limiting Results with LIMIT
Progress Tracking
Log in to save this lesson and continue from where you left off.
Getting Just What You Need
Sometimes you don't need every row. You want the top 10 customers, the 5 most recent orders, or just a sample to check your query logic. LIMIT lets you cap how many rows come back.
This returns the 5 highest-paid employees.
Dialect Differences
This is one of the biggest syntax differences across SQL dialects:
- PostgreSQL / MySQL —
LIMIT 10at the end of the query - SQL Server —
SELECT TOP 10 ...immediately afterSELECT - Oracle —
FETCH FIRST 10 ROWS ONLYat the end, afterORDER BY
StrataScratch supports all 4 dialects. If you're practicing for a company that uses SQL Server, switch to that dialect and practice with TOP instead of LIMIT.
Without ORDER BY, Results Are Unpredictable
You can use row-limiting without ORDER BY, but the rows you get back are arbitrary — it depends on how the database stores the data:
-- PostgreSQL / MySQL
SELECT * FROM techcorp_workforce LIMIT 3;
-- Oracle
SELECT * FROM techcorp_workforce FETCH FIRST 3 ROWS ONLY;
-- SQL Server
SELECT TOP 3 * FROM techcorp_workforce;All three return 3 rows in no guaranteed order. For consistent results, always pair with ORDER BY.
Without ORDER BY, row-limiting is handy for a quick peek: "Show me a few rows so I can see what this table looks like."
The Top N Pattern
The most common use of row-limiting is the "Top N" pattern: ORDER BY combined with your dialect's limit clause.
| id | cust_id | order_date | order_details | total_order_cost |
|---|---|---|---|---|
| 1 | 3 | 2019-03-04 | Coat | 100 |
| 2 | 3 | 2019-03-01 | Shoes | 80 |
| 3 | 3 | 2019-03-07 | Skirt | 30 |
| 4 | 7 | 2019-02-01 | Coat | 25 |
| 5 | 7 | 2019-03-10 | Shoes | 80 |
Find the 3 most expensive orders. You need both sorting and limiting.
This returns the three most expensive orders.
The same pattern works for any "top N" question — here's 5 most recently hired employees across all three dialects:
-- PostgreSQL / MySQL
SELECT first_name, last_name, joining_date
FROM techcorp_workforce
ORDER BY joining_date DESC
LIMIT 5;
-- Oracle
SELECT first_name, last_name, joining_date
FROM techcorp_workforce
ORDER BY joining_date DESC
FETCH FIRST 5 ROWS ONLY;
-- SQL Server
SELECT TOP 5 first_name, last_name, joining_date
FROM techcorp_workforce
ORDER BY joining_date DESC;Hour Of Highest Gas Expense
| index | weather | hour | travel_distance | gasoline_cost |
|---|---|---|---|---|
| 0 | cloudy | 7 | 24.47 | 1.13 |
| 1 | cloudy | 23 | 23.67 | 1.99 |
| 2 | sunny | 17 | 20.93 | 0.86 |
| 3 | rainy | 2 | 29.58 | 0.85 |
| 4 | rainy | 7 | 16.11 | 0.95 |
Find the hour with the highest gasoline cost. Assume there's only 1 hour with the highest gas cost.
Skipping Rows with OFFSET
OFFSET lets you skip a number of rows before returning results. It's used for pagination, and the syntax splits along the same dialect lines:
-- PostgreSQL / MySQL
SELECT
first_name,
last_name,
salary
FROM techcorp_workforce
ORDER BY salary DESC
LIMIT 5 OFFSET 5;-- Oracle / SQL Server
SELECT
first_name,
last_name,
salary
FROM techcorp_workforce
ORDER BY salary DESC
OFFSET 5 ROWS FETCH NEXT 5 ROWS ONLY;Both skip the top 5 earners and return employees ranked 6–10. Oracle and SQL Server share the same OFFSET … FETCH NEXT pagination syntax.
Pagination example:
-- PostgreSQL / MySQL
LIMIT 10 OFFSET 0 -- Page 1
LIMIT 10 OFFSET 10 -- Page 2
LIMIT 10 OFFSET 20 -- Page 3
-- Oracle / SQL Server
OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY -- Page 1
OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY -- Page 2
OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY -- Page 3OFFSET can be slow on large tables because the database still has to process all the skipped rows. For high-performance pagination, other techniques like keyset pagination are better.
Putting It All Together
Row limiting works with all the other clauses you've learned. The full structure per dialect:
-- PostgreSQL / MySQL
SELECT columns
FROM table
WHERE conditions
ORDER BY column
LIMIT n;-- Oracle
SELECT columns
FROM table
WHERE conditions
ORDER BY column
FETCH FIRST n ROWS ONLY;-- SQL Server
SELECT TOP n columns
FROM table
WHERE conditions
ORDER BY column;| id | cust_id | order_date | order_details | total_order_cost |
|---|---|---|---|---|
| 1 | 3 | 2019-03-04 | Coat | 100 |
| 2 | 3 | 2019-03-01 | Shoes | 80 |
| 3 | 3 | 2019-03-07 | Skirt | 30 |
| 4 | 7 | 2019-02-01 | Coat | 25 |
| 5 | 7 | 2019-03-10 | Shoes | 80 |
Find the 3 highest-value orders placed by customer 15.
This should return Dresses (200), Boats (100), and Jeans (80).
Key Takeaways
- Row-limiting clauses (
LIMIT,TOP,FETCH FIRST) cap the number of rows returned. - Always pair with
ORDER BYfor predictable results. OFFSETskips rows — useful for pagination, but slow on large tables.- Syntax varies by dialect:
LIMIT(PostgreSQL/MySQL),TOP(SQL Server),FETCH FIRST(Oracle). - Query position varies:
LIMIT/FETCH FIRSTgo at the end;TOPgoes right afterSELECT.
What's Next
Congratulations! You've completed the core SQL Foundations module. You now know how to SELECT data, filter with WHERE, use logical operators, match patterns, handle NULLs, sort results, and limit output. Now, join us in the next module where you'll learn aggregations and grouping — the tools that turn raw rows into business insights.