MySQL Limit Clause
Notes:
MySQL Limit Caluse:
- limit clause allows us to control how many records a select query should return
- To control how many records a select query should return; we take help of limit clause
Basic Syntax:
select * from tablename limit [offset,] recordsCount;
- offset indicates how many rows to skip from the beginning of the table
- recordsCount indicates how many records the select query should return
Note:
- default value of the offset is 0
- offset = 0 indicates no offset (i.e. no records to skip)
Ex:
select * from tbl_faculty limit 0,2;
- it returns 2 records from the beginning of tbl_faculty table
- i.e. It starts from the 1st row of tbl_faculty table and it returns 2 records
select * from tbl_faculty limit 2;
- it returns 2 records from the beginning of tbl_faculty table
- i.e. It starts from the 1st row of tbl_faculty table and it returns 2 records
select * from tbl_faculty limit 2,4;
- It starts from the 3rd row of tbl_faculty table and it returns 4 records
select * from tbl_faculty order by id desc limit 2,4;
- It starts from the 3rd row of the tbl_faculty table; which is sorted by id column in descending order and it returns 4 records
Let's understand: How to limit the number of records in the result set. or How to control the # of records a select query should return:
Interview Questions: