Nested Subquery for Latest Record
DoorDash ☯️ Medium SQLJoins
Beginner Mode

Objective

Given a table named events with the following columns:

Column Type Description
id INTEGER The unique identifier for each event
event_name VARCHAR The name of the event
user_id INTEGER The identifier of the user associated with the event
event_date DATE The date when the event occurred
status VARCHAR The status of the event

Write an SQL query to fetch the most recent event for each user. The result should include the event_name, user_id, event_date, and status of these latest events, sorted in ascending order by user_id.

Additional information

  • Each user may have multiple events recorded in the events table.
  • The event_date for each event is unique per user.
  • If a user has multiple events on the same latest date, you can return any one of them.
  • Ensure that your query is optimized for performance, especially with large datasets.

Examples

Example 1:

Input:

events
event_dateevent_nameidstatususer_id
2023-01-01login1success101
2023-01-02purchase2completed101
2023-01-01login3success102
2023-01-03logout4success101
2023-01-02purchase5failed102

Output:
event_dateevent_namestatususer_id
2023-01-03logoutsuccess101
2023-01-02purchasefailed102
Quick Solution

Code Environment

Sign in or try as guest to run your code.

Sign In

Track

Question Difficulty Company Access
Need more practice in this area? Explore more questions →