What is the use of LIMIT and OFFSET in SQL?
Question
Share
Sign Up to our social questions and Answers to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers to ask questions, answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
We will be using below Students table to explain the
Example Queries:-
If there are a large number of tuples satisfying the query conditions, it might be
resourceful to view only a handful of them at a time.
returned by SQL.
FIRST clauses.
Example Queries to demonstrate LIMIT Clause.
FROM Students
LIMIT 3;
Output of the query gives First three Student Records.
FROM Students
ORDER BY age
LIMIT 3;
Output of the query gives three Student Records in order of Ascending Ages.
The LIMIT operator can be used in situations such as the above, where we need to
find the top N students in a class and based on any condition statements.
Using LIMIT along with OFFSET
LIMIT x OFFSET y simply means skip the first y entries and then return the next x
entries.
OFFSET can only be used with the ORDER BY clause. It cannot be used on its own.
OFFSET value must be greater than or equal to zero. It cannot be negative, else
returns error.
Example query to demonstrate LIMIT and OFFSET.
FROM Students
LIMIT 3 OFFSET 2
ORDER BY roll_no;
Returns 3 Student Records skipping first two records in Table.