cswork / libraryOpen practice workspace

Coding questions · Meta

Query carpool ride metrics

This question evaluates proficiency in data manipulation and analytical querying using SQL and Python to compute ride-sharing metrics such as daily completed pooled rides, average seats utilized, and cancellation rate.

Question

For a ride-sharing product with carpool capability, answer a series of SQL questions (e.g., daily completed pooled rides, average seats utilized, cancellation rate). Assume typical trip, user, and pooling tables; no CTEs are required.

Daily Carpool Ride Metrics

Using the tables below for a ride-sharing product with carpool capability, compute daily carpool metrics:

  1. The number of completed pooled rides per calendar date.
  2. The average number of seats utilized per completed pooled ride per date (seats utilized are the sum of seats from bookings with status = 'completed' for that pool; exclude 'no_show' and any cancelled bookings from utilized seats).
  3. The booking cancellation rate per date, defined as (number of bookings created that day with status in ('cancelled_by_rider', 'cancelled_by_driver')) divided by total bookings created that day.

Output one row per calendar date that appears either in completed pooled ride end_time or in booking created_at.

Table users

columntypeconstraints
user_idINTEGERPRIMARY KEY
nameVARCHAR(100)NOT NULL
signup_dateDATENOT NULL

Sample data:

namesignup_dateuser_id
Alice Rider2025-07-201
Bob Rider2025-07-212
Carol Rider2025-07-223
Dave Rider2025-07-234
Eve Rider2025-07-245
Frank Rider2025-07-256
Dan Driver2025-07-011001
Dana Driver2025-07-011002

Table drivers

columntypeconstraints
driver_idINTEGERPRIMARY KEY
user_idINTEGERNOT NULL, FOREIGN KEY REFERENCES users(user_id)

Sample data:

driver_iduser_id
11001
21002

Table pooled_rides

columntypeconstraints
pool_idINTEGERPRIMARY KEY
driver_idINTEGERNOT NULL, FOREIGN KEY REFERENCES drivers(driver_id)
start_timeTIMESTAMPNOT NULL
end_timeTIMESTAMP
statusVARCHAR(20)NOT NULL
seat_capacityINTEGERNOT NULL

Sample data:

driver_idend_timepool_idseat_capacitystart_timestatus
12025-08-01 08:45:001042025-08-01 08:00:00completed
12025-08-01 10:10:001142025-08-01 09:15:00completed
2NULL1232025-08-01 17:30:00cancelled
22025-08-02 09:30:001332025-08-02 08:30:00completed
22025-08-02 18:50:001422025-08-02 18:00:00completed

Table pool_bookings

columntypeconstraints
booking_idINTEGERPRIMARY KEY
pool_idINTEGERNOT NULL, FOREIGN KEY REFERENCES pooled_rides(pool_id)
rider_idINTEGERNOT NULL, FOREIGN KEY REFERENCES users(user_id)
seats_bookedINTEGERNOT NULL
statusVARCHAR(30)NOT NULL
created_atTIMESTAMPNOT NULL
cancel_timeTIMESTAMP

Sample data:

booking_idcancel_timecreated_atpool_idrider_idseats_bookedstatus
100NULL2025-08-01 07:20:001011completed
101NULL2025-08-01 07:25:001021completed
1022025-08-01 07:45:002025-08-01 07:40:001032cancelled_by_rider
103NULL2025-08-01 07:55:001041completed
104NULL2025-08-01 08:50:001151completed
105NULL2025-08-01 08:55:001161completed
1062025-08-01 09:05:002025-08-01 09:00:001121cancelled_by_driver
107NULL2025-08-01 09:05:001131no_show
1082025-08-01 17:30:002025-08-01 17:00:001211cancelled_by_driver
1092025-08-01 17:30:002025-08-01 17:05:001241cancelled_by_driver
110NULL2025-08-02 07:50:001311completed
111NULL2025-08-02 07:55:001352completed
1122025-08-02 08:10:002025-08-02 08:05:001331cancelled_by_rider
113NULL2025-08-02 17:30:001421completed
114NULL2025-08-02 17:35:001461completed
1152025-08-02 17:45:002025-08-02 17:40:001441cancelled_by_driver

Expected output

One row per calendar date. completed_pooled_rides and avg_seats_utilized are based on pooled ride end_time for rides with status = 'completed'. avg_seats_utilized is the average, over completed pools that day, of the total completed seats booked in that pool (sum of seats_booked where booking status = 'completed'). cancellation_rate is cancellations divided by total bookings created that day (statuses counted as cancellations: 'cancelled_by_rider', 'cancelled_by_driver'). Exclude 'no_show' from cancellations and from utilized seats.

avg_seats_utilizedcancellation_ratecompleted_pooled_ridesride_date
2.50.422025-08-01
2.50.333322025-08-02

Hints

  1. Treat each pooled ride (vehicle journey) as one unit and sum completed seat bookings per pool to get seats utilized.
  2. Use conditional aggregation on pool_bookings.created_at to compute cancellations and total bookings per day.
  3. Join the per-day aggregates without CTEs by using inline subqueries and a FULL OUTER JOIN on date.
  4. Count cancellations using statuses IN ('cancelled_by_rider','cancelled_by_driver'); exclude 'no_show' from cancellations and from utilized seats.