SQL
Lab
Uber Uber Interview Question 01

Trip
Completion
Rate

Calculate the trip completion rate per city.

Table Schema

Inspect
Table

interactive
ColumnType
trip_idinteger
driver_idinteger
rider_idinteger
cityvarchar
statusvarchar

Sample Data

Input
Output

Sample Input: trips
trip_iddriver_idrider_idcitystatus
1201101NYCcompleted
2202102NYCcancelled
3203103SFcompleted
4204104SFcompleted
5205105NYCcompleted
6206106SFcancelled
7207107NYCcompleted
8208108NYCcancelled
Expected Output
citycompletion_rate
NYC60.00
SF66.67

SQL Editor

Run
Query

postgresql
Waiting for query

citycompletion_rate
NYC60.00
SF66.67

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 city, ROUND(100.0 * SUM(CASE WHEN status = 'completed' THEN 1 ELSE 0 END) / COUNT(*), 2) AS completion_rate
FROM trips
GROUP BY city;

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