easy

Top Earning Driver Per City

Top Earning Driver Per City

You are a data analyst at ZipRide, a ride-hailing platform operating across multiple cities. The operations team wants to recognise and reward the highest-earning driver in each city for the previous month as part of their driver incentive programme.

You have access to two tables:

Table: drivers

Column

Type

Description

driver_id

INT

Unique driver identifier

driver_name

VARCHAR

Full name of the driver

city

VARCHAR

City where the driver operates

Table: rides

Column

Type

Description

ride_id

INT

Unique ride identifier

driver_id

INT

References the driver who completed the ride

ride_date

DATE

Date the ride was completed

fare

NUMERIC(8,2)

Fare amount collected for the ride

Your Task:

Write a query that returns the single top-earning driver per city, based on their total fare earnings across all rides. Include the city, driver name, and their total earnings rounded to 2 decimal places.

Sort results by city alphabetically.

Expected Output Columns: city, driver_name, total_earnings

1
2
3
4
5
Preparing...
Top Earning Driver Per City

Top Earning Driver Per City You are a data analyst at ZipRide , a ride-hailing platform operating across multiple cities. The operations team wants to recognise and reward the highest-earning driver in each city for the previous month as part of their driver incentive programme. You have access to two tables: Table: drivers Column Type Description driver_id INT Unique driver identifier driver_name VARCHAR Full name of the driver city VARCHAR City where the driver operates Table: rides Column Type Description ride_id INT Unique ride identifier driver_id INT References the driver who completed the ride ride_date DATE Date the ride was completed fare NUMERIC(8,2) Fare amount collected for the ride Your Task: Write a query that returns the single top-earning driver per city , based on their total fare earnings across all rides. Include the city, driver name, and their total earnings rounded to 2 decimal places. Sort results by city alphabetically. Expected Output Columns: city , driver_name , total_earnings

Table Setup (SQL Schema)

-- DDL: Create tables
CREATE TABLE drivers (
    driver_id   INT PRIMARY KEY,
    driver_name VARCHAR(150),
    city        VARCHAR(100)
);

CREATE TABLE rides (
    ride_id    INT PRIMARY KEY,
    driver_id  INT REFERENCES drivers(driver_id),
    ride_date  DATE,
    fare       NUMERIC(8, 2)
);

-- DML: Insert sample data
INSERT INTO drivers (driver_id, driver_name, city) VALUES
(1,  'Amir Hasan',      'Singapore'),
(2,  'Brenda Loh',      'Singapore'),
(3,  'Chen Wei',        'Singapore'),
(4,  'Diana Patel',     'Kuala Lumpur'),
(5,  'Edward Tan',      'Kuala Lumpur'),
(6,  'Fatima Malik',    'Kuala Lumpur'),
(7,  'George Ng',       'Bangkok'),
(8,  'Hannah Reyes',    'Bangkok'),
(9,  'Ivan Cruz',       'Bangkok'),
(10, 'Julia Santos',    'Jakarta'),
(11, 'Kevin Ong',       'Jakarta'),
(12, 'Layla Ahmad',     'Jakarta');

INSERT INTO rides (ride_id, driver_id, ride_date, fare) VALUES
(1,  1,  '2024-03-01', 12.50),
(2,  1,  '2024-03-03', 18.00),
(3,  1,  '2024-03-07', 22.75),
(4,  2,  '2024-03-02', 15.00),
(5,  2,  '2024-03-05', 9.50),
(6,  3,  '2024-03-01', 30.00),
(7,  3,  '2024-03-04', 27.50),
(8,  3,  '2024-03-09', 19.00),
(9,  4,  '2024-03-02', 14.00),
(10, 4,  '2024-03-06', 21.00),
(11, 5,  '2024-03-03', 33.50),
(12, 5,  '2024-03-08', 28.00),
(13, 5,  '2024-03-11', 17.25),
(14, 6,  '2024-03-04', 11.00),
(15, 6,  '2024-03-07', 16.50),
(16, 7,  '2024-03-01', 24.00),
(17, 7,  '2024-03-05', 19.50),
(18, 8,  '2024-03-03', 38.00),
(19, 8,  '2024-03-06', 31.00),
(20, 8,  '2024-03-10', 14.50),
(21, 9,  '2024-03-02', 10.00),
(22, 9,  '2024-03-08', 22.00),
(23, 10, '2024-03-01', 17.00),
(24, 10, '2024-03-04', 29.50),
(25, 10, '2024-03-09', 13.00),
(26, 11, '2024-03-03', 41.00),
(27, 11, '2024-03-07', 36.50),
(28, 11, '2024-03-11', 22.00),
(29, 12, '2024-03-02', 18.50),
(30, 12, '2024-03-06', 15.00);
Home Videos Quiz Blog