Junior — Middle
Can you explain how the LIMIT OFFSET construction works in SQL to limit the amount of data returned and offset the result?
sobes.tech AI
Answer from AI
In SQL, the LIMIT OFFSET construction is used to restrict the number of rows returned and to offset the start of the selection.
LIMITsets the maximum number of rows to return.OFFSETindicates how many rows to skip from the beginning of the result.
For example, the query:
SELECT * FROM users ORDER BY id LIMIT 10 OFFSET 20;
means: return 10 rows starting from the 21st (since offset 20 skips the first 20 rows).
This is often used for implementing pagination. For example, to get the third page with 10 items per page, you would do LIMIT 10 OFFSET 20.
Different DBMSs may have slightly different syntax, but the idea is the same — limit and offset the selection.