Select, Filter and Sort · Problem 12 of 118

Products, Page Two

MediumSelect, Filter and SortShop data

The website shows 10 products per page, most expensive first. Return page 2: the products ranked 11 to 20 by price.

Return product_id, name and price, sorted by price from high to low and then product_id.

Hint 1

LIMIT 10 takes ten rows, and OFFSET 10 skips the first ten.

Hint 2

Paging only makes sense with a complete ORDER BY. Add product_id as a tie-breaker.

Reference solution

SELECT product_id, name, price
FROM products
ORDER BY price DESC, product_id
LIMIT 10 OFFSET 10;

Page n of size s is LIMIT s OFFSET (n - 1) * s. For page 2 of 10 that is LIMIT 10 OFFSET 10. Pages are only stable if the order is fully determined: with ties in price and no tie-breaker, a product could appear on two pages or on none. Large offsets also get slow, because the database still reads the skipped rows.