Overview
Build page 2 of a product catalog: sort with ORDER BY, take one page with LIMIT and OFFSET, and break ties so pages never overlap.
- ORDER BY ... DESC
- A second column breaks ties
- LIMIT and OFFSET for pages
Short Course
ORDER BY sorts (DESC for largest first), LIMIT keeps the first rows and
OFFSET skips some: that's pagination. Trap: rows with equal values come back
in any order, so a product can show up on two pages. Add a second sort column.
SELECT name, price
FROM products
ORDER BY price DESC, name -- ties broken by name
LIMIT 3 OFFSET 3; -- page 2, 3 per page
Task
Return the name and price of the products on page 2 of the catalog, with 3
products per page, most expensive first and ties sorted by name. In the
database below, Lamp and Rug both cost 30.
Database
Sample data from the first test. The other tests use different rows, so write a query, not a list of answers.
| idINTEGER | nameTEXT | priceREAL |
|---|---|---|
| 1 | Lamp | 30.0 |
| 2 | Desk | 120.0 |
| 3 | Pen | 2.0 |
| 4 | Chair | 80.0 |
| 5 | Mug | 8.0 |
| 6 | Rug | 30.0 |
| 7 | Clock | 25.0 |
Test Suites
Pending
Next
Output