Course contentsShow
← Data Science
Lesson 877 of 2,14522. SQL FoundationsFree lesson

LIMIT: Restricting the Number of Rows Returned

Use LIMIT to cap result sets, essential for testing queries and pagination in large datasets.

LIMIT: Restricting the Number of Rows Returned

You'll learn how to control exactly how many rows your query returns, making testing faster and results more manageable.

What Is LIMIT?

The LIMIT clause in SQL caps the number of rows returned by your query. Think of it like asking for a sample from a huge warehouse: instead of receiving all million products, you say "just show me the first 10." This is especially useful when working with large datasets where retrieving everything would be slow, overwhelming, or unnecessary.

Why Use LIMIT?

When you're testing a new query on a table with thousands or millions of rows, you don't want to wait forever or crash your system. LIMIT lets you:

  • Test queries safely by previewing just a handful of rows
  • Speed up development by working with small, fast result sets
  • Implement pagination (showing results in batches, like "page 1 of 100")
  • Preview data structure without downloading entire tables

Basic Syntax

SELECT column1, column2
FROM table_name
LIMIT 5;

This returns only the first 5 rows from your query result.

Combining with Other Clauses

LIMIT works with everything you've learned:

SELECT product_name, price
FROM products
WHERE price > 50
ORDER BY price DESC
LIMIT 10;

This finds products over $50, sorts them by price (highest first), and shows only the top 10.

Placement Matters

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

Key Takeaway: Use LIMIT to control how many rows your query returns—essential for testing queries efficiently and working with large datasets without overwhelming your system.