Skip to content
ML Academy

SQL and Python

Interview question

  • midlevel
  • 8 minsuggested time
  • 2Data analyst, Data scientist

A dashboard shows the cancellation rate by fare class from a table bookings(booking_id, fare_class, status). Here is the query: SELECT fare_class, SUM(CASE WHEN status = 'cancelled' THEN 1 ELSE 0 END) / COUNT(*) AS cancel_rate FROM bookings GROUP BY fare_class. Every fare class shows 0. Some rows have status IS NULL (the status feed lagged). Fix the query and explain what denominator you chose and why.

Tests: Integer division, conditional aggregation, NULL handling in rates, and making the denominator an explicit decision rather than an accident of COUNT(*) versus COUNT(column).

Sign in to practise: answers are saved before model answers are shown.