We may require to display some part of the records returned by a query specifying a range. What is the range to be returned we can specify by saying the starting position of record and number of records. We will pass this starting and
number of records along with the SQL LIMIT command to restrict the records within
that range. We will see the syntax of this query in our student MySQL table.
SELECT * FROM `student` LIMIT 0, 10
We have specified here to return 10 records starting from 0 or from the first
record. Same way we can ask for 10 records starting from 20th record like
this
SELECT * FROM `student` LIMIT 20, 10
This will return 10 records from 21st record. That is from 21st record to
30th record.
| id | name | class | mark |
|---|---|---|---|
| 21 | Babby John | Four | 69 |
| 22 | Reggid | Seven | 55 |
| 23 | Herod | Eight | 79 |
| 24 | Tiddy Now | Seven | 78 |
| 25 | Giff Tow | Seven | 88 |
| 26 | Crelea | Seven | 79 |
| 27 | Big Nose | Three | 81 |
| 28 | Rojj Base | Seven | 86 |
| 29 | Tess Played | Seven | 55 |
| 30 | Reppy Red | Six | 79 |
SELECT * FROM `student` ORDER BY class limit 0,10
In the above example the 10 records will be displayed based on alphabetical order of class column. Some sample records are here.
| id | name | class | mark |
|---|---|---|---|
| 23 | Herod | Eight | 79 |
| 7 | My John Rob | Five | 78 |
| 18 | Honny | Five | 75 |
| 8 | Asruid | Five | 85 |
| 31 | Marry Toeey | Four | 88 |
| 21 | Babby John | Four | 69 |
| 1 | John Deo | Four | 75 |
| 16 | Gimmy | Four | 88 |
| 15 | Tade Row | Four | 88 |
| 10 | Big John | Four | 55 |
Read more on WHERE to filter records based matching Conditions
List of students with Mark above 80 ( ten records only by using LIMIT )SELECT * FROM `student` WHERE mark >80 LIMIT 0,10
List of students with Mark above 80 ( ten records only ) in the order of lowest to highest.
SELECT * FROM `student` WHERE mark >80 ORDER BY mark LIMIT 0,10
In the order of Highest to lowest
SELECT * FROM `student` WHERE mark >80 ORDER BY mark DESC LIMIT 0,10
SELECT * FROM `student` ORDER BY id DESC limit 0,5
Here id is an auto incremented unique field.
SELECT * from student ORDER BY dt DESC limit 0,5
Here dt is time stamp or date and time field.
require "config.php";// Database connection
//////////////////////////////
if($stmt = $connection->query("SELECT id, name ,class, mark FROM student limit 0,5")){
echo "No of records : ".$stmt->num_rows."<br>";
echo "<table class='table table-striped'>
<tr class='info'> <th> ID</th><th>Name</th><th>Class</th><th>Mark</th></tr>";
while ($row = $stmt->fetch_assoc()) {
echo "<tr><td>$row[id]</td><td>$row[name]</td><td>$row[class]</td><td>$row[mark] </td></tr>";
}
echo "</table>";
}else{
echo $connection->error;
}
exit;
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.
| 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 | |