LIMIT
- SQL course
- Beginner
- Lesson 16/115
- 4 min
Limit the number of rows returned.
LIMIT
With LIMIT you cap the number of rows returned. The most common use is fetching the top or bottom N records.
SELECT name, price
FROM products
ORDER BY price DESC
LIMIT 3;This returns the 3 most expensive products. LIMIT comes after ORDER BY.
name, price is selected from products. The result is sorted by price in descending order. Only the first 3 rows are returned.
LIMIT — details
LIMIT is usually paired with ORDER BY; otherwise which rows come back is undefined. To get any 5 rows from a table you write LIMIT 5.
For the 3 best-selling products you write ORDER BY sales DESC LIMIT 3; without sorting, "the first 3" has no meaning.
Practice LIMIT
Select the name and price of the 3 most expensive products.
Starter code
-- top 3 by price
SELECT name, price
FROM products
ORDER BYSample solution
SELECT name, price
FROM products
ORDER BY price DESC
LIMIT 3Expected output
name price Çanta 25 Defter 12 Kalem 5
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is LIMIT most often paired with?
- WHERE
- GROUP BY
- HAVING
- ORDER BY Correct answer
Explanation: LIMIT is usually paired with ORDER BY.
2 Top 3 records
SELECT name FROM products
ORDER BY price DESC
____;
SELECT name FROM products
ORDER BY price DESC
LIMIT 3;
Explanation: LIMIT 3 is the right answer.
3 LIMIT: match the terms
| Term | Meaning |
|---|---|
LIMIT |
cap rows |
ORDER BY |
sort |
DESC |
descending |
OFFSET |
skip |