cswork / libraryOpen practice workspace

Coding questions · Waymo

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

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.

Hint: 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.

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

Table users

columntypeconstraints
user_idINTEGERPRIMARY KEY
cityTEXTNOT NULL

Sample data:

cityuser_id
Phoenix107
SF101
LA104
Phoenix110
SF102
Seattle109
Phoenix103
LA105
SF108
Seattle106

Table rides

columntypeconstraints
ride_idINTEGERPRIMARY KEY
ride_dateDATENOT NULL
ride_ratingINTEGER
user_idINTEGERNOT NULL

Sample data:

ride_dateride_idride_ratinguser_id
2024-02-2670034101
2024-03-0370113103
2024-01-1570014106
2024-02-297008NULL105
2024-02-1270052107
2024-02-0370025106
2024-02-2770135108
2024-02-2070063106
2024-03-0170095107
2023-12-287004NULL110
2024-03-0270124108
2024-01-3170074109
2024-03-037010NULL108
2024-02-2570145102

Expected output

user_idcity
102SF
104LA
106Seattle
109Seattle
110Phoenix

Hints

  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.
  2. The window ends on the latest ride_date present in rides, not on today's date, and both of its ends are inclusive.