Katkat

SQL: Users With No Rides in the Most Recent 7-Day Window, Including Non-Riders

WaymoData Scientist · Onsite

Last updated

Write a PostgreSQL query that lists users with no rides in the most recent 7-day window, including users who never rode. Tests anchoring a date window to the data, inclusive boundaries, and anti-join logic that keeps users without matching rides.

A ride service stores its users and their rides in two tables. Find the inactive users: users who took no ride during the most recent 7-day window. Users who registered but have never taken a ride at all are inactive too and must be included.

Input Tables

users: one row per registered user.

ColumnTypeMeaning
user_idINTEGERPrimary key
cityTEXTCity the user belongs to; never NULL

rides: one row per ride.

ColumnTypeMeaning
ride_idINTEGERPrimary key
ride_dateDATEDate of the ride; never NULL
ride_ratingINTEGERRider's rating of the ride from 1 to 5; NULL if not rated
user_idINTEGERThe rider; always references an existing users.user_id

Output Contract

  • Write one read-only PostgreSQL query (a single SELECT, optionally with CTEs).
  • The 7-day window is the 7 calendar days ending on the latest ride_date present in rides, inclusive: from that date minus 6 days through that date.
  • Return one row per inactive user with columns user_id and city.
  • Order the result by user_id ascending.

Example

users

user_idcity
1SF
2SF
3Phoenix
4Phoenix
5LA

rides

ride_idride_dateride_ratinguser_id
1012024-07-0151
1022024-07-0942
1032024-07-14NULL3
1042024-07-1531
1052024-07-0854

The latest ride date is 2024-07-15, so the window is 2024-07-09 through 2024-07-15. Users 1, 2 and 3 rode inside it (user 2 exactly on the first day). User 4's only ride is one day before the window, and user 5 never rode.

Expected result:

user_idcity
4Phoenix
5LA

Constraints and Clarifications

  • Both window boundaries are inclusive.
  • A ride counts toward activity whether or not it has a rating; ride_rating plays no part in this question.
  • A user with several rides in the window appears nowhere in the output; each inactive user appears exactly once.
  • If rides is empty, there is no window and no ride, so every user is inactive.
  • user_id is unique in users, so the ordering is total.

Hints

Hint 1

Keep the users who never rode: Check whether your join or filter silently drops users who have no rows in `rides`, or whose only rides fall outside the window.