3.2 — Result Paging with LIMIT
What You'll Learn
- How to use
LIMIT
to control how many rows you see - How this compares to viewing the top rows in Excel
From Excel to SQL: Viewing Top Rows
In Excel, you might scroll to see just the first few rows. In SQL, you use LIMIT
.
SQL:
sql
SELECT * FROM sales
ORDER BY Amount DESC
LIMIT 100;
LIMIT 100
shows only the top 100 rows after sorting.
When to Use LIMIT
- Previewing Data: When you want to quickly check the first few rows of a large table without loading everything.
- Performance: Limiting results can make queries run faster, especially on big tables.
- Exporting Samples: If you need to export a sample of your data for testing or sharing.
- Avoiding Overload: Many query tools and database UIs automatically apply a default
LIMIT
(like 100 or 1000 rows) to prevent accidentally loading millions of rows.
Tip:
If you run a query without LIMIT
, your tool may still only show the first 100 or 1000 rows by default. Adding LIMIT
ensures you control exactly how many rows you see.
Using OFFSET for Paging
You can use OFFSET
to skip rows:
sql
SELECT * FROM sales
LIMIT 5 OFFSET 10;
- Skips first 10 rows, shows rows 11-15.
Key Points
- Use
LIMIT
to control how many rows you see. - Combine with
ORDER BY
for best results.
Next Steps
Next, you’ll learn how to create calculated columns—just like using formulas in Excel.