SQL · Level 1 Sorting

SQL ORDER BY, LIMIT and OFFSET

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

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

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.

Sample data from the first test. The other tests use different rows, so write a query, not a list of answers.

products 7 rows
idINTEGERnameTEXTpriceREAL
1Lamp30.0
2Desk120.0
3Pen2.0
4Chair80.0
5Mug8.0
6Rug30.0
7Clock25.0
solution.sql
Test Suites Pending
Output