MySQL LIMIT restricts the number of rows returned by a query. To return the first 10 rows:
SELECT id, name, class, mark
FROM student
LIMIT 10;
To skip the first 20 rows and return the next 10:
SELECT id, name, class, mark
FROM student
LIMIT 20, 10;
The first value is the offset; the second is the number of rows to return. An offset of 20 skips rows 1–20, so the result starts with row 21.
Return a fixed number of rows:
SELECT column_list
FROM table_name
LIMIT row_count;
Example:
SELECT id, name, class, mark
FROM student
LIMIT 8;
This returns at most eight rows.
MySQL supports two common forms for skipping rows.
SELECT id, name, class, mark
FROM student
LIMIT 20, 10;
SELECT id, name, class, mark
FROM student
LIMIT 10 OFFSET 20;
Both queries skip 20 rows and return up to 10 rows.
OFFSET 0 starts with the first row, while OFFSET 20 starts after the first 20 rows.Combine ORDER BY with LIMIT when you need a meaningful first or last set of rows.
SELECT id, name, class, mark
FROM student
ORDER BY class ASC, id ASC
LIMIT 10;
The query first defines the order, then returns the first ten rows from that ordered result.
The WHERE clause filters records before LIMIT is applied.
SELECT id, name, class, mark
FROM student
WHERE mark > 80
ORDER BY mark DESC, id ASC
LIMIT 10;
This returns at most ten students with marks above 80, highest mark first.
To return the highest mark:
SELECT id, name, mark
FROM student
ORDER BY mark DESC, id ASC
LIMIT 1;
To return the top three rows:
SELECT id, name, mark
FROM student
ORDER BY mark DESC, id ASC
LIMIT 3;
This query returns the second row after sorting:
SELECT id, name, mark
FROM student
ORDER BY mark DESC, id ASC
LIMIT 1 OFFSET 1;
If two students share the highest mark, the second row can have the same mark as the first. That is different from finding the second distinct highest mark. See second-highest record examples for that distinction.
If an auto-increment ID increases with each insert, the latest five IDs can be returned with:
SELECT id, name, class, mark
FROM student
ORDER BY id DESC
LIMIT 5;
See MySQL AUTO_INCREMENT.
If the table has a date-time column such as dt, sort by that value and use a secondary key to make ties deterministic:
SELECT id, name, dt
FROM student
ORDER BY dt DESC, id DESC
LIMIT 5;
For page-based pagination, calculate the offset from the page number and number of rows per page.
offset = (page_number - 1) * rows_per_page
For 10 rows per page:
| Page | Offset | Query range |
|---|---|---|
| 1 | 0 | Rows 1-10 |
| 2 | 10 | Rows 11-20 |
| 3 | 20 | Rows 21-30 |
Page 3 can use:
SELECT id, name, class, mark
FROM student
ORDER BY id ASC
LIMIT 10 OFFSET 20;
PHP MySQL Pagination
Validate paging values as integers and bind them with PDO::PARAM_INT.
<?php
require 'config.php';
$page=filter_input(
INPUT_GET,
'page',
FILTER_VALIDATE_INT,
[
'options' => [
'default' => 1,
'min_range' => 1
]
]
);
$per_page=10;
$offset=($page-1)*$per_page;
$stmt=$dbo->prepare(
"SELECT id,name,class,mark
FROM student
ORDER BY id ASC
LIMIT :limit OFFSET :offset"
);
$stmt->bindValue(
':limit',
$per_page,
PDO::PARAM_INT
);
$stmt->bindValue(
':offset',
$offset,
PDO::PARAM_INT
);
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
echo htmlspecialchars(
(string)$row['name'],
ENT_QUOTES,
'Windows-1252'
).'<br>';
}
The page number is application input, so it is validated before the offset is calculated. Output values are escaped before being inserted into HTML.
OFFSET pagination is simple and works well for many applications, but large offsets can become expensive because the database may still need to locate and skip many earlier rows.
For example:
SELECT id, name
FROM student
ORDER BY id
LIMIT 20 OFFSET 100000;
For very large or continuously growing datasets, keyset pagination can be more efficient when the application can continue from the last seen ordered key:
SELECT id, name
FROM student
WHERE id > 5000
ORDER BY id ASC
LIMIT 20;
The best approach depends on the application, indexes, data size and navigation requirements.
LIMIT works with row positions in the ordered result. It does not filter by ID values.
This skips 20 result rows:
SELECT id, name
FROM student
ORDER BY id
LIMIT 10 OFFSET 20;
This filters by an actual column value:
SELECT id, name
FROM student
WHERE id BETWEEN 21 AND 30
ORDER BY id;
See SQL BETWEEN for value ranges.
LIMIT is common in MySQL and several other database systems, but row-limiting syntax is not identical across all SQL databases. Other systems may use syntax such as FETCH FIRST, OFFSET ... FETCH or TOP.
When writing portable SQL, check the syntax supported by the target database rather than assuming the MySQL LIMIT form works everywhere.
Without a deterministic ORDER BY, rows can appear in an unpredictable order between requests.
OFFSET counts result rows to skip. It has no direct relationship with an ID column.
In MySQL comma syntax, LIMIT 20,10 means offset 20 and row count 10.
It returns the second sorted row. Tied values can mean the second row has the same value as the first.
Large offsets can require substantial work. Consider indexes and alternative pagination designs when datasets become large.
In a normal MySQL SELECT statement, ORDER BY appears before LIMIT.
It returns at most 10 rows from the query result.
It skips the first 20 rows and returns up to the next 10 rows.
Yes. Offset 0 starts with the first row, offset 1 skips one row, and offset 20 skips the first 20 rows.
Use ORDER BY whenever the identity or sequence of the returned rows matters, especially for top-N queries and pagination.
It returns the second row in the sorted result, which is not necessarily the second distinct highest value when ties exist.
Yes. Validate the values as integers and bind them using PDO::PARAM_INT.
It can become expensive at large offsets. Depending on the application and indexes, keyset pagination may be more efficient for deep navigation.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| Rajkumar | 12-09-2011 |
| I am just looking for an equivalent to top clause in MS SQL SERVER 2005 that follows where clause (something similar to Limit in Mysql). I require this to be used in another application to retriev the latest record from DB. The clause that comes next to select cant be used. Can you please help me. | |
| Anil kumar rawat | 08-08-2012 |
| Hello Guys, Show tables limit 4,4 not working What will be command for limit in mysql | |
| subhend | 08-08-2012 |
| This is not for listing of tables of a database. | |
| ankur | 08-04-2014 |
| Helo Guys, i have a table, table name is "employees" and i have insrt duplicates values more than two times. now i want remove that values but want to put one value only waht i do in sql server query. | |
| Shahbaz Ahmed Bhatti | 12-08-2015 |
| Plus2net was my first tutorial website for learning php 8 years ago, now i again need to check query an di found this website again very gooooooooood healp cheers plus2net team | |
| sheddie | 26-02-2018 |
| how can you display all the marks below 80 on the webpage or any other limit? I.e query to show data in a table from a database given a limit of actual data in the database | |
| smo1234 | 14-02-2019 |
| SELECT * FROM `student` WHERE mark < 80 | |