LIMIT and TOP
Restrict how many rows are returned
✅ What You Will Learn
LIMIT (MySQL, PostgreSQL) and TOP (SQL Server) both restrict how many rows a query returns. This is essential when you want the "top 5 products", "latest 10 orders", or just want to preview data without pulling millions of rows.
In real analytics work, databases often contain millions of records. Running SELECT * without LIMIT can crash your query tool. Always use LIMIT when exploring data.
📋 The orders table used in examples
| order_id | customer_name | product | amount | order_date |
|---|---|---|---|---|
| 1001 | Rahul Sharma | Laptop | 45000 | 2026-01-15 |
| 1002 | Priya Verma | Mobile Phone | 18000 | 2026-01-16 |
| 1003 | Amit Kumar | Headphones | 3500 | 2026-01-16 |
| 1004 | Sneha Kapoor | Laptop | 52000 | 2026-01-17 |
| 1005 | Vikram Singh | Tablet | 28000 | 2026-01-18 |
Syntax
Examples
📌 Key Points to Remember
- ✓LIMIT goes at the very end of the query
- ✓Always use ORDER BY with LIMIT — otherwise the "top N" rows are random
- ✓SQL Server uses TOP n before the column list, not at the end
- ✓OFFSET lets you skip rows for pagination
- ✓Use LIMIT 10 when first exploring a new table to preview its data safely
🏢 Real-World Application
LIMIT is used constantly in production analytics. When a data analyst first connects to a new database table with millions of rows, they always start with SELECT * FROM table_name LIMIT 10 to preview the structure without overloading the system. Dashboards showing "Top 10 Products This Week" use ORDER BY revenue DESC LIMIT 10. Paginated reports showing 25 rows per page use LIMIT 25 OFFSET 0 for page 1, LIMIT 25 OFFSET 25 for page 2, and so on. LIMIT is your safeguard on large datasets.
⚠️ Common Mistakes to Avoid
❓ Frequently Asked Questions
What is LIMIT in SQL?
LIMIT restricts how many rows a query returns. SELECT * FROM orders LIMIT 5 returns only the first 5 rows. It is used with ORDER BY to get top-N results, and with OFFSET for pagination.
What is the difference between LIMIT and TOP in SQL?
They do the same thing but with different syntax. MySQL and PostgreSQL use LIMIT at the end: SELECT col FROM table LIMIT 10. SQL Server uses TOP at the start: SELECT TOP 10 col FROM table.
How does OFFSET work with LIMIT?
OFFSET skips a number of rows before starting. LIMIT 10 OFFSET 20 skips the first 20 rows and returns the next 10. This is the standard way to implement pagination in SQL.
How do I get the top 10 records in SQL?
Use ORDER BY to sort in the order you want, then LIMIT 10. For the highest values: SELECT * FROM orders ORDER BY amount DESC LIMIT 10. For most recent: SELECT * FROM orders ORDER BY order_date DESC LIMIT 10.
Does LIMIT improve SQL query performance?
LIMIT reduces the number of rows sent back to the client, which reduces network overhead. But the database may still scan the entire table to find and sort rows before applying LIMIT. Use WHERE and proper indexes for real performance gains.
✏️ Practice Exercise
Write a query to find the 5 most recent orders by order_date.
Learn SQL with Live Trainer Guidance
These tutorials give you the theory. Our live SQL course at EVIKA Academy, Noida teaches you to apply SQL on real company datasets — with a trainer who uses it daily at MakeMyTrip.