Skip rows with OFFSET
An SQL exercise from Querydrill. Solve it with a SQL editor in your browser, checked on the rows it returns and again on changed data, and it is checked the moment you submit.
Try it yourself
Sort the products by price, most expensive first. Return the name and price of the 4th, 5th and 6th product in that list, in that order.
products (8 rows)
| id | name | category | price |
|---|---|---|---|
| 1 | Desk Lamp | Home | 35 |
| 2 | Office Chair | Home | 180 |
| 3 | Notebook | Stationery | 4.5 |
| 4 | Pen Set | Stationery | 12 |
| 5 | Headphones | Electronics | 95 |
| 6 | Webcam | Electronics | 60 |
| 7 | Mug | Home | 9 |
| 8 | Backpack | Travel | 70 |
Free, no account needed. PostgreSQL runs in your browser and your answer is checked again on changed data.
What this exercise teaches
LIMIT with OFFSET is how result pages are built: page 2 with 3 rows per page is LIMIT 3 OFFSET 3. It only makes sense together with ORDER BY, because without a sort the rows have no fixed order to skip through.
A hint if you get stuck
LIMIT sets how many rows come back, OFFSET how many are skipped first. To start at the 4th row, skip 3.
Background you need
Common mistakes on this exercise
- Skips one row too many.
- Sorted cheapest first.
Each of these returns a result that looks plausible. The checker catches them because it runs your query on changed data as well.
Keep going on the practice page
Your first 8 exercises on Querydrill are free, with no account. The practice page has the full editor, timed mode and all the exercises in order. A free account shows 3 reference solutions a day; Pro unlocks every exercise, every solution, timed mode and the certificate.
Open this exerciseMore exercises in Basics
- Pick two columns
- Filter rows
- Top three earners
- Count paid orders
- Each country once
- Small orders
- Two countries
- A date range
- Combine AND with OR
- Pattern match
- No manager
- Replace NULL with a default
- Sort on two columns
- Calculated column
- Glue text together
- Label with CASE
- Upper case and length
- Search without caring about case (ILIKE)
- Full years of service (age)