KatInterview

Rating Trends Across a Rider's First and Third Months by City

SQL & DataModel answer
MediumModel answerSQL & Datasqlpostgresqlwindow-functions

Last updated

Write a PostgreSQL query returning, per city, the mean per-ride rating in each rider's first and third months with rides, handling skipped months, unrated rides and weighting.

A ride-hailing team wants to see whether riders score their trips differently as they stay with the product. For each city, compare the typical rating given during a rider's first month of use with the rating given during their third month of use.

Definitions

A rider's active months are the calendar months (year and month together) in which they took at least one ride. Number them in time order: the earliest is the rider's 1st active month, the next is the 2nd, and so on. Months with no rides are skipped, so the 3rd active month may be much later than two calendar months after the 1st.

Tables

users

ColumnTypeNotes
user_idINTEGERPrimary key
cityTEXTNever NULL

rides

ColumnTypeNotes
ride_idINTEGERPrimary key
ride_dateDATENever NULL
ride_ratingINTEGER1 to 5, or NULL if the rider did not rate
user_idINTEGERAlways references users.user_id

Task

Write a single read-only PostgreSQL query (one SELECT, CTEs allowed) that returns one row per city having at least one ride:

  • city (TEXT)
  • month_1_avg_rating (NUMERIC): mean ride_rating across all rides that fall in their rider's 1st active month, for riders in that city, rounded to 2 decimals.
  • month_3_avg_rating (NUMERIC): the same for the 3rd active month, rounded to 2 decimals; NULL when the city has no rated ride in that position.

Sort by city ascending.

Rules

  • Average over individual rides, not over per-user averages. A rider with three rides in the month supplies three values.
  • Every ride, rated or not, counts when deciding which months are active. Unrated rides are excluded only from the averages.
  • Months are identified by year and month, so January 2024 and January 2025 are different months.
  • Riders with fewer than three active months still count toward the month-1 column.
  • If every month-1 ride in a city is unrated, month_1_avg_rating is NULL (the city still appears).
  • Riders with no rides have no effect; a city with no rides is absent.

Example

users

user_idcity
1SF
2SF
3LA

rides

ride_idride_dateride_ratinguser_id
12024-01-0551
22024-01-2051
32024-01-3121
42024-02-1041
52024-04-0221
62024-04-18NULL1
72024-02-0322
82024-03-09NULL2
92024-05-0132
102024-03-1553
112024-03-2843

Expected output:

citymonth_1_avg_ratingmonth_3_avg_rating
LA4.50NULL
SF3.502.50

Rider 1's active months are Jan, Feb, Apr; rider 2's are Feb, Mar (one unrated ride, still active), May; rider 3 has only Mar. SF month 1 is (5+5+2+2)/4 = 3.50, whereas averaging the two riders' own averages would wrongly give 3.00.

What to deliver

Provide the query and explain how you number the months, why that choice is correct, and how NULL ratings and the weighting rule are handled.

Hints

Hint 1

Work in two phases: first label each ride with the number of its rider's active month, then aggregate ratings by city and that number.

Hint 2

Number months, not rides. Truncate ride_date to the month (keeping the year) and use a ranking function that gives equal values the same rank and leaves no gaps; compare ROW_NUMBER, RANK and DENSE_RANK.

Hint 3

Do not filter out NULL ratings before ranking, because an unrated ride still makes its month active. Let AVG skip NULLs, and use FILTER (or CASE) to get both columns in one GROUP BY.