Module 1: SQL Foundations25 min

Limiting Results with LIMIT

Progress Tracking

Log in to save this lesson and continue from where you left off.

Log in

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 / MySQLLIMIT 10 at the end of the query
  • SQL ServerSELECT TOP 10 ... immediately after SELECT
  • OracleFETCH FIRST 10 ROWS ONLY at the end, after ORDER BY
Practice in Your Target Dialect

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:

SQL
-- 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.

Useful for Data Exploration

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.

Table: orders
idcust_idorder_dateorder_detailstotal_order_cost
132019-03-04Coat100
232019-03-01Shoes80
332019-03-07Skirt30
472019-02-01Coat25
572019-03-10Shoes80
1
Try the Top N Pattern

Find the 3 most expensive orders. You need both sorting and limiting.

Tables: orders

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:

SQL
-- 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

Table: lyft_rides
indexweatherhourtravel_distancegasoline_cost
0cloudy724.471.13
1cloudy2323.671.99
2sunny1720.930.86
3rainy229.580.85
4rainy716.110.95
2
Hour Of Highest Gas Expense
View solution

Find the hour with the highest gasoline cost. Assume there's only 1 hour with the highest gas cost.

Tables: lyft_rides

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:

SQL
-- PostgreSQL / MySQL
SELECT
  first_name,
  last_name,
  salary
FROM techcorp_workforce
ORDER BY salary DESC
LIMIT 5 OFFSET 5;
SQL
-- 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:

SQL
-- 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 3
OFFSET Can Be Slow on Large Tables

OFFSET 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:

SQL
-- PostgreSQL / MySQL
SELECT columns
FROM table
WHERE conditions
ORDER BY column
LIMIT n;
SQL
-- Oracle
SELECT columns
FROM table
WHERE conditions
ORDER BY column
FETCH FIRST n ROWS ONLY;
SQL
-- SQL Server
SELECT TOP n columns
FROM table
WHERE conditions
ORDER BY column;
Table: orders
idcust_idorder_dateorder_detailstotal_order_cost
132019-03-04Coat100
232019-03-01Shoes80
332019-03-07Skirt30
472019-02-01Coat25
572019-03-10Shoes80
3
Top 3 Orders for a Specific Customer

Find the 3 highest-value orders placed by customer 15.

Tables: orders

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 BY for predictable results.
  • OFFSET skips 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 FIRST go at the end; TOP goes right after SELECT.

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.