# #1407 Top Travellers

#### <https://leetcode.com/problems/top-travellers/>

{% tabs %}
{% tab title="MS SQL Server" %}

```sql
SELECT u.name,
    CASE 
        WHEN SUM(distance) IS NULL THEN 0
        ELSE SUM(distance)
    END travelled_distance
FROM Users u LEFT JOIN Rides r 
ON u.id = r.user_id
GROUP BY u.name
ORDER BY travelled_distance DESC, u.name ASC
```

{% endtab %}
{% endtabs %}
