AnalystPath

Same Opponent for First and Last Match of a Day

SQLHardSenior level~15 min

Problem

Table: `matches`

| Column Name | Type |
|---|---|
| home_id | int |
| away_id | int |
| played_at | datetime |

`(home_id, away_id, played_at)` is the primary key. Each row is a chess match between two players at a moment in time.

A player participates as either the home or away side. For each player and each calendar day they played, look at their earliest match and their latest match that day. Report the `player_id` of every player whose first and last match on at least one day were against the same opponent. Return each qualifying player once, in any order, in a column named `player_id`. Assume no player has two matches at the exact same timestamp on the same day.

Tables

Example rows — the live problem includes the full dataset.

matches
home_idaway_idplayed_at
122023-03-01 09:00:00
312023-03-01 12:00:00
212023-03-01 17:00:00

Expected output

Your answer should return 3 rows with the columns player_id.

Starter code (SQL)

SELECT *
FROM matches;

Solve this SQL question free

Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.

Solution & explanation

Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.

Sign up free to unlock

Related SQL questions