LIMIT & OFFSET¶
LIMIT and OFFSET are SQL clauses that allow you to control the number of rows returned by a SELECT statement. They are particularly useful when you need to paginate or retrieve a specific portion of the result set.
LIMIT¶
The LIMIT clause restricts the number of rows returned by a query. The syntax for using LIMIT is:
OFFSET¶
The OFFSET clause is used in combination with LIMIT to skip a specific number of rows before starting to return the rows. The syntax for using OFFSET is:
You can also use OFFSET without LIMIT:
Examples¶
Consider the following Test table:
With the following records:
Using LIMIT¶
Retrieve the first 3 rows from the Test table:
Result:
Using LIMIT and OFFSET¶
Retrieve the next 4 rows after the first 3 rows from the Test table:
Result:
Using OFFSET without LIMIT¶
Retrieve all rows after the first 2 rows from the Test table:
Result: