SQL
Lab
Google Google Maps Interview Question 01

Ride
Completion
Rate

Calculate the completion rate of rides per city.

Table Schema

Inspect
Table

interactive
ColumnType
ride_idinteger
user_idinteger
cityvarchar
statusvarchar

Sample Data

Input
Output

Sample Input: rides
ride_iduser_idcitystatus
1101NYCcompleted
2102NYCcancelled
3103SFcompleted
4104SFcancelled
5105NYCcompleted
6106SFcompleted
7107NYCcancelled
8108NYCcompleted
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: Count completed rides per city.
Hint 02: Divide by total rides in that city.
Hint 03: Use 100.0 or a decimal cast to avoid integer division.

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 rides
GROUP BY city;

Explanation

Step By
Step

01

Group rides by city.

02

Count completed rides with a CASE expression.

03

Divide completed rides by all rides and round the percentage.

Related Questions

Keep
Solving