- LIMIT clause is used to specify the number of records to return.
- LIMIT clause is very useful for retriving data from large tables it can provide multipage results easily.
- Let us assume that we have to retrive 1-50 records from a STUDENT table. Then the following SQL query would be used for this purpose:
$sql = “SELECT * FROM STUDENT LIMIT 50”;
- The above query will give first 50 records of STUDENT table after execution.
- Now suppose you wanted to show results 20-25. With the OFFSET keyword its just as easy, the following query will do:
$sql = “SELECT * FROM STUDENT LIMIT 6 OFFSET 19”;
- You could also use a shorter syntax to achieve the same result:
$sql = “SELECT * FROM Orders LIMIT 19, 6”;
- Notice that the numbers are reversed when you use a comma.