The LIMIT Clause

How to cap the number of rows returned

← All topics

What is LIMIT?

LIMIT restricts how many rows a query returns. Without it, a SELECT statement returns every matching row in the table — which could be millions of rows on a large dataset. LIMIT lets you cap that number.

SELECT column1, column2
FROM table_name
LIMIT 10;

This returns at most 10 rows, no matter how many rows are in the table.

Common uses

Previewing a table — when you first explore an unfamiliar table, LIMIT 5 or LIMIT 10 gives you a quick sample without pulling the entire dataset.

-- Just peek at the first 5 rows
SELECT *
FROM coffee_shop_sales
LIMIT 5;

Finding the top N results — combine ORDER BY with LIMIT to get the highest or lowest values in a column.

-- The 3 most expensive rentals
SELECT car_model, daily_rate_usd
FROM car_rental_records
ORDER BY daily_rate_usd DESC
LIMIT 3;

💡 Clause order

LIMIT always comes last — after SELECT, FROM, WHERE, and ORDER BY.

ClausePosition
SELECT1st
FROM2nd
WHERE3rd (if needed)
ORDER BY4th (if needed)
LIMITLast

LIMIT with WHERE and ORDER BY

All three clauses work together. WHERE filters the rows first, ORDER BY sorts them, and LIMIT keeps only the top N from the sorted result.

-- Top 5 most expensive rooms costing over $120/night
SELECT guest_name, room_type, price_per_night
FROM hotel_room_bookings
WHERE price_per_night > 120
ORDER BY price_per_night DESC
LIMIT 5;

Common mistakes

Putting LIMIT before ORDER BY — this causes a syntax error. LIMIT must always be the last clause in the query.

Expecting a specific set of rows without ORDER BY — without ORDER BY, the database can return any N rows in any order. If you want the "top" results, always pair LIMIT with ORDER BY.

Using LIMIT to count rowsLIMIT only controls how many rows are returned to you; it doesn't count them. Use COUNT(*) if you need a total.


Practice: LIMIT

Apply what you learned above. Write a query that matches each task.

LIMIT

Table Schema

Table:
Columns:

Task

Tip: Ctrl + Enter to submit