SQL LIMIT and OFFSET

LIMIT: Limits the number of rows returned by a query
OFFSET: Specifies how many rows to skip before returning the results

Here’s an example of using LIMIT and OFFSET in SQL

SELECT * FROM table LIMIT 5 OFFSET [Number of rows to skip];

In the above SQL query, it will skip the first x number of rows mentioned in OFFSET and then select the next 5 rows.

example if we ran the following query on the table

select * from table limit 2 offset 1; // offset will skip first one row. and limit will select the next 2 rows i.e 102,103

101
102
103
104
105
SELECT FROM Movies LIMIT 5 OFFSET 2: Skips the first two rows and returns the first five results

The LIMIT and OFFSET clauses are useful for pagination queries, especially when working with large tables. However, all databases do not support the LIMIT clause, and using large OFFSET values can slow down database servers.

Leave a Reply

Your email address will not be published. Required fields are marked *