MySQL LIMIT Clause

MySQL LIMIT Clause
The LIMIT clause in MySQL is used to restrict the number of rows returned by a SELECT query.
It’s essential for pagination, top-N queries, and performance optimization.
What is LIMIT?
Controls how many rows are returned
Often combined with
ORDER BYVery useful for large tables
LIMIT affects only the result set, not the data stored.
Basic Syntax
Example
- Returns first 5 rows
LIMIT with OFFSET
Used to skip rows before returning results.
Syntax
Example
- Skips first 5 rows
- Returns next 5 rows (rows 6–10)
LIMIT + ORDER BY (Very Important)
To get top or bottom records, always use ORDER BY.
Top 3 Highest Salaries
Lowest 5 Marks
LIMIT for Pagination
Common formula:
Example: Page 3 (10 records per page)
LIMIT with WHERE Clause
WHEREfilters rows before LIMIT is applying.
LIMIT with GROUP BY
- Top 3 departments by employee count
LIMIT vs TOP (Interview)
LIMIT and TOP both restrict the number of rows returned, but they belong to different SQL systems.LIMIT is used in MySQL, PostgreSQL, and SQLite, while TOP is used in SQL Server.LIMIT is written at the end of the query, whereas TOP is written immediately after SELECT.
LIMIT Example
TOP Example
Both queries return 10 rows, but the syntax depends on the database.
MySQL →
LIMITSQL Server →
TOPPostgreSQL →
LIMIT
Common Mistakes
- Using LIMIT without ORDER BY (results can be unpredictable)
- Confusing offset and count
- Expecting LIMIT to delete data
- Using negative values
Interview Questions & MCQs (Very Important)
Q1. What does LIMIT do?
A) Deletes rows
B) Updates rows
C) Restricts number of rows returned
D) Sorts rows
Answer: C
Q2. Which query returns first 10 rows?
A) Correct
B) Incorrect
Answer: A
Q3. What does LIMIT 5, 10 mean?
A) First 5 rows
B) Rows 5 to 10
C) Skip 5, return next 10
D) Return 10, skip 5
Answer: C
Q4. LIMIT is best using with which clause?
A) WHERE
B) GROUP BY
C) ORDER BY
D) HAVING
Answer: C
Q5. Is LIMIT supporting in standard SQL?
A) Yes
B) No (DB-specific)
Answer: B
Q6. Which is correct for pagination?
A) Correct
B) Incorrect
Answer: A
Real-Life Use Cases
- Pagination in web apps
- Top-N reports
- Sampling large datasets
- Faster previews of data
Summary
LIMITrestricts number of rows returnedSupports OFFSET
Best used with ORDER BY
Essential for pagination
DB-specific but very common
Important for SQL exams & interviews
