SQL
Lab
Uber Uber Interview Question 06

Cancellation
Rate
by Rider

Find riders with cancellation rate greater than 50%.

Table Schema

Inspect
Table

interactive
ColumnType
trip_idinteger
rider_idinteger
statusvarchar

Sample Data

Input
Output

Sample Input: trips
trip_idrider_idstatus
1101completed
2101cancelled
3101cancelled
4102completed
5102cancelled
6103cancelled
Expected Output
rider_id
101
103

SQL Editor

Run
Query

postgresql
Waiting for query

rider_id
101
103

Hints

Unlock
Clues

Hint 01: Identify the grouping level required by the output.
Hint 02: Aggregate with COUNT, SUM, AVG, or a window function as needed.
Hint 03: Filter after aggregation with HAVING or after ranking with an outer query.

Solution

Locked
Answer

Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.

SELECT rider_id
FROM trips
GROUP BY rider_id
HAVING 1.0 * SUM(CASE WHEN status = 'cancelled' THEN 1 ELSE 0 END) / COUNT(*) > 0.5;

Explanation

Step By
Step

01

Read the expected output columns to determine the final grain.

02

Aggregate or rank the input rows to calculate the requested metric.

03

Filter, sort, and alias the final columns to match the output.

Related Questions

Keep
Solving