SQL Notes
Learn how to restrict the number of rows returned by a SQL query using the LIMIT clause, improve query performance, and retrieve only the records you need.
Databases can hold thousands, millions, or billions of records. Most of the time, you don't need all of them. Maybe you just want the first few, the top performers, or a quick preview while testing.
Real examples:
- Your shopping app shows 10 products per page, not 1 million all at once.
- Twitter-like apps load 20 posts, then 20 more. That's pagination.
- Your dashboard shows the top 5 earners, not everyone's paycheck.
- You're testing a query and need a quick sample, not the entire dataset.
Pulling everything when you need just a bit is wasteful. It kills performance, burns memory, and frustrates users.
To solve this problem, SQL provides the LIMIT Clause.
The LIMIT clause caps how many rows come back from your query. It's everywhere: web apps, dashboards, reports, pagination.
Why is LIMIT Important?
Picture a Products table with:
500,000 Products
If you run this without LIMIT:
SELECT *
FROM Products;You get all 500k rows back.
That's bad:
- Slow execution
- Increased memory usage
- Higher network traffic
- Poor user experience
Now use LIMIT:
SELECT *
FROM Products
LIMIT 10;Just 10 rows.
Much better:
- Faster queries
- Better performance
- Reduced resource consumption
- Improved application responsiveness
Basic LIMIT Syntax
The general syntax is:
SELECT ColumnName
FROM TableName
LIMIT Number;Example:
SELECT *
FROM Students
LIMIT 3;Only three rows are returned.
Understanding the Syntax
Here's what each part does:
SELECT *
FROM Students
LIMIT 3;SELECT — Gets your data.
FROM — Which table.
LIMIT — How many rows max.
3 — The number: stop after 3.
The database will hand you 3 rows and stop.
Creating a Sample Table
Let's set up a test table:
CREATE TABLE Students (
StudentID INT,
Name VARCHAR(100),
Age INT
);Add some students:
INSERT INTO Students VALUES
(1, 'Rahul', 20),
(2, 'Priya', 21),
(3, 'Amit', 19),
(4, 'Neha', 22),
(5, 'Rohan', 20);Your table now looks like this:
| StudentID | Name | Age |
|---|---|---|
| 1 | Rahul | 20 |
| 2 | Priya | 21 |
| 3 | Amit | 19 |
| 4 | Neha | 22 |
| 5 | Rohan | 20 |
Retrieving the First Few Rows
This query:
SELECT *
FROM Students
LIMIT 2;Gives you:
| StudentID | Name |
|---|---|
| 1 | Rahul |
| 2 | Priya |
Just the first two rows. Simple.
LIMIT with ORDER BY
LIMIT really shines when you pair it with ORDER BY. This is where the magic happens.
Watch:
SELECT *
FROM Students
ORDER BY Age DESC
LIMIT 3;Process:
Sort Records
↓
Return Top 3 RowsResult:
The three oldest students are returned.
Finding Top Paid Employees
Say you need the top earners:
SELECT *
FROM Employees
ORDER BY Salary DESC
LIMIT 5;You get:
The five employees with the highest salaries are displayed.
This is a common business reporting requirement.
LIMIT with WHERE
You can mix LIMIT with WHERE to be even more surgical.
Like this:
SELECT *
FROM Products
WHERE Price > 5000
LIMIT 10;Process:
WHERE → Filter Records
↓
LIMIT → Return First 10 RowsOnly matching records are considered.
LIMIT with Multiple Conditions
Getting fancy:
SELECT *
FROM Employees
WHERE Department = 'IT'
ORDER BY Salary DESC
LIMIT 3;Returns:
The top three highest-paid IT employees are displayed.
Using LIMIT 1
Sometimes you just need one.
Like checking if something exists:
SELECT *
FROM Students
LIMIT 1;Hands back:
Only the first row is returned.
Handy for:
- Testing queries
- Checking table structure
- Retrieving a single record
LIMIT with OFFSET
OFFSET skips ahead. Combined with LIMIT, it's the backbone of pagination.
Here's the pattern:
SELECT *
FROM Students
LIMIT 5 OFFSET 10;Process:
Skip First 10 Rows
↓
Return Next 5 RowsThis is widely used in pagination systems.
Pagination Example
Think of a store with product pages:
Page 1:
SELECT *
FROM Products
LIMIT 10 OFFSET 0;Page 2:
SELECT *
FROM Products
LIMIT 10 OFFSET 10;Page 3:
SELECT *
FROM Products
LIMIT 10 OFFSET 20;That's how you scroll through pages without loading everything.
LIMIT in Different Database Systems
Different databases have different keywords. Here's the rundown:
MySQL
SELECT *
FROM Students
LIMIT 5;PostgreSQL
SELECT *
FROM Students
LIMIT 5;SQLite
SELECT *
FROM Students
LIMIT 5;SQL Server
Uses TOP (we'll cover this next):
SELECT TOP 5 *
FROM Students;Oracle
Uses FETCH (more on this later):
SELECT *
FROM Students
FETCH FIRST 5 ROWS ONLY;Real-World Example
Let's say you're building an online store homepage.
You've got a Products table with:
1,000,000 Products
Your homepage needs:
Show Latest 20 ProductsYour query:
SELECT *
FROM Products
ORDER BY ProductID DESC
LIMIT 20;You show the freshest 20.
Performance stays snappy instead of crawling.
Common Errors
Forgetting ORDER BY
If you do this:
SELECT *
FROM Students
LIMIT 5;You'll get 5 rows, but which 5? Nobody knows. Could be random.
Add ORDER BY when it matters:
ORDER BY StudentIDso results are predictable.
Negative LIMIT Values
This won't work:
LIMIT -5Databases will throw an error. Negative limits don't make sense.
Assuming LIMIT Sorts Data
Mistake:
SELECT *
FROM Students
LIMIT 5;LIMIT just caps the count.
Sorting requires:
ORDER BYUsing Large LIMIT Values
This is pointless:
LIMIT 1000000If you're capping at a million rows, you've beat the whole point.
Best Practices
Combine LIMIT with ORDER BY
Do this:
SELECT *
FROM Products
ORDER BY Price DESC
LIMIT 10;Use Pagination for Large Datasets
For big datasets:
LIMIT 20 OFFSET 40;It feels faster to the user.
Retrieve Only Required Columns
Better:
SELECT Name, Age
FROM Students
LIMIT 5;Don't pull stuff you won't use. Saves bandwidth.
Use Small Limits During Testing
When you're testing:
LIMIT 5Grab 5 rows fast so you can see if your logic works.
Common Interview Questions
What is the purpose of LIMIT?
It caps how many rows come back. Useful for performance and pagination.
Can LIMIT be used with ORDER BY?
Absolutely.
It's super common — sorting first, then grabbing the top N.
What is OFFSET?
It skips ahead. Skip 10, return the next 5 — that's how pagination works.
Does LIMIT sort data?
Nope.
You need ORDER BY for sorting. LIMIT just stops after N rows.
Summary
LIMIT is essential. It restricts output, speeds up queries, and powers pagination everywhere online.
In this lesson, you learned:
- What LIMIT is
- Why LIMIT is important
- Basic syntax
- LIMIT with ORDER BY
- LIMIT with WHERE
- LIMIT with OFFSET
- Pagination
- Database-specific alternatives
- Common mistakes
- Best practices
You'll see LIMIT in almost every real application because showing everything at once is a bad user experience. Break it into pages.
Next Step
Continue to the next lesson:
TOP Clause →
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for LIMIT Clause.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this SQL Complete Guide topic.
Search Terms
sql-complete-guide, sql complete guide, sql, complete, guide, filtering, limit, clause
Related SQL Complete Guide Topics