Top Two Per Shelf

SQL practice problem 49 of 67 · Window Functions · Medium · 25 coins for your first accepted solve

For every category, return its two most expensive items (fewer if it has fewer). Break price ties by name.

Return category, name, price. Row order does not matter.

Schema

CREATE TABLE items (
  id        INT PRIMARY KEY,
  name      TEXT NOT NULL,
  category  TEXT NOT NULL,
  price     NUMERIC(8,2) NOT NULL
);

Write your query in the browser, run it on the sample data, then submit it against hidden test data.

Previous: Running Tab · Next: First Purchase · All problems