Time Between Logins

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

For every login, return player_id, logged_at, and hours_to_next — the hours until that player's next login, rounded to 1 decimal. A player's last login has NULL.

Sort by player_id, then logged_at.

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 logins (
  player_id  INT NOT NULL REFERENCES players(id),
  logged_at  TIMESTAMP NOT NULL
);

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

Previous: Quartiles · Next: Growth Curve · All problems