Top Travellers

2pts
Lyft

Problem Statement

Lyft is running a rewards program and needs a leaderboard of its users.

Write an SQL query to report the distance traveled by each user.

Rules:

  • Return the name and the total traveled_distance.
  • If a user has not traveled any distance, you must report it as 0.
  • Results must be sorted by traveled_distance in descending order.
  • If two users have the same traveled_distance, sort them by name in ascending order.
Tests your understanding of
Basic SQL, JOIN, Aggregation and COALESCE

Input Tables

users
id(INTEGER)name(VARCHAR)
1Alice
2Bob
3Charlie
4David
7Eve
13Jonathan
19Elvis
rides
id(INTEGER)user_id(INTEGER)distance(INTEGER)
11120
22317
33222
47100
513312
61950
77120
819400
97230

Expected Output

name(VARCHAR)traveled_distance(INTEGER)
Elvis450
Eve450
Bob317
Jonathan312
Charlie222
Alice120
David0

Tags

EasyBasic SQLJOINAggregationCOALESCE
10-15 min
68%

Hints