The OFFSET function in SQL is used to skip a specified number of rows before starting to return rows from a query. It is often used in conjunction with the LIMIT clause to implement pagination.
Basic Syntax:
SELECT column1, column2
FROM table_name
ORDER BY column
OFFSET number_of_rows_to_skip ROWS
FETCH NEXT number_of_rows_to_return ROWS ONLY;
Example:
SELECT name
FROM employees
ORDER BY id
OFFSET 10 ROWS
FETCH NEXT 5 ROWS ONLY;
In this example:
- The query skips the first 10 rows.
- It then fetches the next 5 rows from the
employeestable, ordered byid.
Use Case:
This is useful for paginating results in applications, allowing users to view a specific "page" of data.
If you have a specific scenario or need further clarification, feel free to ask!
