Total Distance Travelled per User, Including Users With No Rides

Quick Overview

Write a SQL query that reports the total ride distance for every user, listing users with no rides as zero and sorting by distance and then name. It tests LEFT JOIN aggregation, turning a NULL sum into zero, and grouping by a unique key when names repeat.

Total Distance Travelled per User, Including Users With No Rides

Company: Point72

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

The `Users` table lists users and the `Rides` table lists each ride with its distance. Report the total distance travelled by each user. Output `name` and `travelled_distance`. ```hint Users without rides Check what `SUM` returns for a user whose joined rows contain no ride at all. ``` ### Input Table(s) `Users` | Column | Type | Meaning | |---|---|---| | id | INT | User identifier | | name | VARCHAR(255) | User name, never NULL, not guaranteed to be unique | Primary key: `id`. `Rides` | Column | Type | Meaning | |---|---|---| | id | INT | Ride identifier | | user_id | INT | User who took the ride (`Users.id`), never NULL | | distance | INT | Distance of the ride, never NULL | Primary key: `id`. ### Output Contract - One row per user in `Users`, with columns `name`, `travelled_distance`, in this order. - `travelled_distance` is the sum of `distance` over the user's rides. Users with no rides appear with `travelled_distance` = `0`. - Order by `travelled_distance` descending, then `name` ascending. Two rows that tie on both keys are identical, so the expected output is unique. ### Example `Users` | id | name | |---|---| | 1 | Alice | | 2 | Bob | | 3 | Alex | | 4 | Bob | | 5 | Dana | `Rides` | id | user_id | distance | |---|---|---| | 1 | 1 | 50 | | 2 | 2 | 30 | | 3 | 2 | 20 | | 4 | 3 | 50 | | 5 | 4 | 10 | Expected output | name | travelled_distance | |---|---| | Alex | 50 | | Alice | 50 | | Bob | 50 | | Bob | 10 | | Dana | 0 | Alice, Bob (id 2) and Alex each travelled 50, so `name` breaks the tie. The two users named Bob are different people: id 2 travelled 30 + 20 = 50 and id 4 travelled 10. Dana has no rides and appears with 0. ### Constraints and Clarifications - Two users can share a name. They are still different users and each gets their own row. - A user with no rides reports `0`, never `NULL`. - The output has exactly one row per row of `Users`. A ride whose `user_id` matches no user is not reported.

Overview: Write a SQL query that reports the total ride distance for every user, listing users with no rides as zero and sorting by distance and then name. It tests LEFT JOIN aggregation, turning a NULL sum into zero, and grouping by a unique key when names repeat.

Point72 logo
Point72
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

The Users table lists users and the Rides table lists each ride with its distance. Report the total distance travelled by each user. Output name and travelled_distance.

Input Table(s)

Users

ColumnTypeMeaning
idINTUser identifier
nameVARCHAR(255)User name, never NULL, not guaranteed to be unique

Primary key: id.

Rides

ColumnTypeMeaning
idINTRide identifier
user_idINTUser who took the ride (Users.id), never NULL
distanceINTDistance of the ride, never NULL

Primary key: id.

Output Contract

  • One row per user in Users , with columns name , travelled_distance , in this order.
  • travelled_distance is the sum of distance over the user's rides. Users with no rides appear with travelled_distance = 0 .
  • Order by travelled_distance descending, then name ascending. Two rows that tie on both keys are identical, so the expected output is unique.

Example

Users

idname
1Alice
2Bob
3Alex
4Bob
5Dana

Rides

iduser_iddistance
1150
2230
3220
4350
5410

Expected output

nametravelled_distance
Alex50
Alice50
Bob50
Bob10
Dana0

Alice, Bob (id 2) and Alex each travelled 50, so name breaks the tie. The two users named Bob are different people: id 2 travelled 30 + 20 = 50 and id 4 travelled 10. Dana has no rides and appears with 0.

Constraints and Clarifications

  • Two users can share a name. They are still different users and each gets their own row.
  • A user with no rides reports 0 , never NULL .
  • The output has exactly one row per row of Users . A ride whose user_id matches no user is not reported.