Home »
MySQL
MySQL LIMIT Clause
MySQL | LIMIT Clause: Learn about the MySQL LIMIT Clause, with its explanation, syntax, and query examples.
Submitted by Apurva Mathur, on September 05, 2022
LIMIT Clause
As the name suggests, LIMIT clause helps us to set a limit to records we want in return as a result.
Suppose you have a large database containing a thousand numbers of data and you want 200 data from starting so in this case LIMIT clause will help us.
LIMIT Clause Syntax
LIMIT Clause Syntax
SELECT statement
FROM table_name
WHERE condition [optional]
LIMIT number;
Case 1: LIMIT clause without WHERE condition
Now we shall understand this by taking a simple database where I have 10 data and my table name is "student_details", and I only want to start 5 records from the table so our query will be:
SELECT * FROM student_details
LIMIT 5;
As you can see after applying the LIMIT clause only starting 5 records are visible.
Case 2: LIMIT clause with WHERE condition
Suppose I only want 5 records where the department is "CSE", so in this case, we'll use both clauses WHERE as well as LIMIT,
SELECT * FROM student_details
WHERE student_department='CSE'
LIMIT 5;
As you can see it only returns 4 records but why? According to the LIMIT set, it should return 5 records then why only 4 records?
This is because the table only has 4 data which has "CSE" as a department, if there were more departments named as "CSE" then it would have returned the result according to the LIMIT set by us, this signifies if you have only one data and LIMIT is set up to some larger number then also this clause won't show any error.
Case 3: LIMIT clause with WHERE condition using AND/OR
Suppose I want to start 3, but the condition is department should be "CSE" and the gender should be "FEMALE".
For this our query will be:
SELECT * FROM student_details
WHERE student_department='CSE' AND Gender='Female'
LIMIT 5;
As you can see our result has 3 data where gender is "female" also the department is "CSE".