Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Most Recent Record Per User

EasySQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Most Recent Record Per User. Start with the requirements and the one table on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Apex Systems needs a clean login audit view from its internal authentication records. The source table may contain multiple login events for the same user, including events with tied or missing timestamps.

Write a PostgreSQL query that returns only the most recent login for each unique user.

Requirements

  1. Exclude records where user_id is NULL.
  2. Use ROW_NUMBER() to rank logins within each user by login_at descending.
  3. Break timestamp ties with the larger login_id.
  4. Return the selected login details, ordered by user_id.

Schema

apex_login_records
ColumnTypeDescription
login_idPKINTUnique login event identifier
user_idINTIdentifier of the user who attempted to log in
login_atTIMESTAMPTZTime of the login event
login_statusVARCHAR(20)Outcome of the login attempt
ip_addressVARCHAR(45)IP address associated with the login attempt
Tablesapex_login_records
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results