OFFSET
OFFSET within SQL is a keyword that provides a way to skip a defined number of data rows in the result set of a query. It's primarily used in conjunction with the LIMIT keyword for implementing paging in SQL queries. The number of rows defined in the OFFSET clause will be skipped before starting to return rows in the query.
Example
Output
Explanation
In this example, OFFSET 3
instructs MySQL to disregard the first 3 rows and LIMIT 5
limits the output to five rows. Therefore the query displays rows 4 to 8 from the Orders table.
Example
Output
Explanation
The example above demonstrated how to obtain records from the orders
table, ordered by order_id
. It limited the output to 5 records, but also used an OFFSET
of 10, which means it skipped the first 10 records in the result set and returned the next 5.
Example
Output
ProductName | Price |
---|---|
Product 11 | 11 |
Product 12 | 12 |
Product 13 | 13 |
Product 14 | 14 |
Product 15 | 15 |
Product 16 | 16 |
Product 17 | 17 |
Product 18 | 18 |
Product 19 | 19 |
Product 20 | 20 |
Explanation
The provided SQL query implements OFFSET
to skip the first 10 records of the Products
table, then uses FETCH NEXT
to return the next 10 records. The data is sorted by Price
. As a result of the query, records 11 through 20 are displayed.
Example
Output
Explanation
In the example provided, the SQL statement selects all rows from the Employees table and orders them by the employee_id. The OFFSET
clause then skips the first 5 rows in the ordered result-set. Therefore, the output displays the data starting from the 6th row.
Example
Output
Explanation
The OFFSET
keyword is used in conjunction with the LIMIT
keyword to skip a number of rows before returning the result set. In this example, the query is skipping the first 3 rows and retrieving the next 5 rows from the Students table. These rows are ordered by the Score in descending order.