Fighters and Shoppers

SQL practice problem 42 of 67 · Subqueries & CTEs · Medium · 25 coins for your first accepted solve

Return the name of every player who has both won at least one match and bought at least one item. Row order does not matter.

Schema

CREATE TABLE players (
  id         INT PRIMARY KEY,
  name       TEXT NOT NULL,
  country    TEXT,            -- NULL = unknown
  level      INT NOT NULL,
  joined_on  DATE NOT NULL,
  mentor_id  INT              -- another player's id, or NULL
);

CREATE TABLE matches (
  id            INT PRIMARY KEY,
  winner_id     INT NOT NULL REFERENCES players(id),
  loser_id      INT NOT NULL REFERENCES players(id),
  played_at     DATE NOT NULL,
  duration_sec  INT NOT NULL
);

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

CREATE TABLE purchases (
  id         INT PRIMARY KEY,
  player_id  INT NOT NULL REFERENCES players(id),
  item_id    INT NOT NULL REFERENCES items(id),
  qty        INT NOT NULL,
  bought_at  DATE NOT NULL
);

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

Previous: Still Waiting · Next: Loyal Customers · All problems