cswork / libraryOpen practice workspace

Coding questions · Waymo

SQL: Count Users With Zero or One Rides per City

Write a PostgreSQL query that counts, per city, the users who have taken zero or one ride in total, including users who never rode. Tests outer joins or subqueries that preserve zero counts, grouping by city, and filtering on aggregated totals.

A ride service stores its users and their rides. A low-frequency user is a user whose total number of rides is 0 or 1. Count the low-frequency users in each city. Users who registered but never took a ride have 0 rides and must be counted.

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
ride_ratingINTEGERRating 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).
  • Return one row per city that has at least one low-frequency user, with columns:
    • city (TEXT);
    • low_frequency_users (BIGINT): the number of users in that city with 0 or 1 rides in total.
  • Order the result by city ascending.

Example

users

user_idcity
1SF
2SF
3SF
4LA
5LA
6Phoenix

rides

ride_idride_dateride_ratinguser_id
12024-07-0151
22024-07-0341
32024-07-04NULL2
42024-07-0534
52024-07-0655
62024-07-0745
72024-07-0855
82024-07-0926
92024-07-1046

Ride totals: user 1 has 2, user 2 has 1, user 3 has 0, user 4 has 1, user 5 has 3, user 6 has 2. The low-frequency users are 2 and 3 (SF) and 4 (LA). Phoenix has none, so it does not appear.

Expected result:

citylow_frequency_users
LA1
SF2

Constraints and Clarifications

  • A ride counts toward the total whether or not it is rated.
  • Cities with no low-frequency user are omitted rather than shown with 0.
  • Each user belongs to exactly one city and is counted at most once.
  • city is unique per output row, so the ordering is total.

Hint: Zero is a count too

Make sure a user with no rides produces a total of 0 rather than disappearing from the data before you filter.

Count Users With Zero or One Rides per City

Table users

columntypeconstraints
user_idINTEGERPRIMARY KEY
cityTEXTNOT NULL

Sample data:

cityuser_id
Austin101
Boston105
Chicago108
Denver110
Seattle113
Austin102
Boston106
Denver111
Seattle114
Austin103
Chicago109
Boston107
Denver112
Seattle115
Austin104

Table rides

columntypeconstraints
ride_idINTEGERPRIMARY KEY
ride_dateDATE
ride_ratingINTEGER
user_idINTEGERNOT NULL

Sample data:

ride_dateride_idride_ratinguser_id
2024-07-0115102
2024-07-0124103
2024-07-0135107
2024-07-0144115
2024-07-0155115
2024-07-026NULL104
2024-07-0274105
2024-07-0285107
2024-07-0291113
2024-07-0310NULL103
2024-07-03113110
2024-07-04125106
2024-07-04134106
2024-07-0414NULL115
2024-07-0515NULL105
2024-07-05165111
2024-07-06174107
2024-07-0618NULL112
2024-07-07194111
2024-07-07203115
2024-07-08213103
2024-07-09222107
2024-07-09234115
2024-07-1024NULL112

Expected output

citylow_frequency_users
Austin3
Chicago2
Denver1
Seattle2

Hints

  1. Zero is a count too: make sure a user with no rides produces a total of 0 rather than disappearing from the data before you filter.
  2. A ride counts toward a user's total whether or not it is rated.