Often, when working with large datasets, you don't need to see every single row to understand the data's structure or to check if your query is returning the expected results. Retrieving thousands or even millions of rows can be slow and consume unnecessary resources, especially during the exploratory phase of data analysis. This is where limiting the number of rows returned by your query becomes extremely useful.
The standard SQL clause for restricting the number of rows in the output is LIMIT
. It's typically placed at the very end of your SELECT
statement.
LIMIT
ClauseThe basic syntax is straightforward:
SELECT column1, column2, ...
FROM table_name
LIMIT number_of_rows;
Here, number_of_rows
is an integer specifying the maximum number of rows you want the query to return.
Imagine we have a products
table:
product_id | product_name | category | price |
---|---|---|---|
1 | Laptop | Electronics | 1200.00 |
2 | Keyboard | Electronics | 75.00 |
3 | Mouse | Electronics | 25.00 |
4 | T-Shirt | Apparel | 20.00 |
5 | Coffee Mug | Home Goods | 15.00 |
6 | Desk Lamp | Home Goods | 45.00 |
If you only wanted to see the first 3 products listed in the table, perhaps just to get a quick sample, you would write:
SELECT product_id, product_name, price
FROM products
LIMIT 3;
The database would process this query and return only the first three rows it encounters (the order might not be guaranteed unless you use ORDER BY
, which we'll discuss next):
product_id | product_name | price |
---|---|---|
1 | Laptop | 1200.00 |
2 | Keyboard | 75.00 |
3 | Mouse | 25.00 |
LIMIT
with ORDER BY
The real power of LIMIT
often comes when combined with the ORDER BY
clause (covered in the next chapter, but essential to mention here). ORDER BY
sorts the results before LIMIT
is applied. This allows you to find things like the "top N" or "bottom N" records based on specific criteria.
For example, to find the 3 most expensive products:
price
in descending order (DESC
).LIMIT
.SELECT product_id, product_name, price
FROM products
ORDER BY price DESC
LIMIT 3;
This query would return:
product_id | product_name | price |
---|---|---|
1 | Laptop | 1200.00 |
2 | Keyboard | 75.00 |
6 | Desk Lamp | 45.00 |
Similarly, to find the 2 least expensive products, you would sort in ascending order (ASC
, which is the default) and then apply LIMIT
:
SELECT product_id, product_name, price
FROM products
ORDER BY price ASC
LIMIT 2;
-- Or simply ORDER BY price LIMIT 2; as ASC is default
Result:
product_id | product_name | price |
---|---|---|
5 | Coffee Mug | 15.00 |
4 | T-Shirt | 20.00 |
LIMIT
to ensure the logic works as expected without waiting for potentially millions of rows.ORDER BY
.While LIMIT
is common (used in PostgreSQL, MySQL, SQLite), some database systems use different syntax:
TOP N
within the SELECT
clause:
SELECT TOP 3 product_id, product_name, price
FROM products;
ROWNUM
in the WHERE
clause (more complex):
SELECT product_id, product_name, price
FROM products
WHERE ROWNUM <= 3;
FETCH FIRST N ROWS ONLY
:
SELECT product_id, product_name, price
FROM products
ORDER BY product_id -- ORDER BY often needed for consistent FETCH results
FETCH FIRST 3 ROWS ONLY;
For this course, we will primarily use the standard LIMIT
syntax, as it's widely adopted and conceptually straightforward for beginners.
Using LIMIT
is a fundamental technique for efficiently interacting with databases, allowing you to sample data and retrieve targeted subsets without overwhelming your system or waiting unnecessarily. In the next section, you'll get hands-on practice writing SELECT
statements incorporating aliases and the LIMIT
clause.
© 2025 ApX Machine Learning