cswork / libraryOpen practice workspace

Coding questions · Uber

Write SQL for fares and age-band counts

This question evaluates SQL competencies in aggregation, filtering, joins, numeric formatting, and precise date-based age calculation, including handling NULL fares, canceled trips, leap-year birthdays, and boundary-age cases.

You have two tables.

Schema:

  • drivers(driver_id VARCHAR PRIMARY KEY, name VARCHAR, date_of_birth DATE)
  • trips(trip_id VARCHAR PRIMARY KEY, driver_id VARCHAR, trip_date DATE, trip_fare_dollars DECIMAL(10,2), trip_status VARCHAR)

Sample data:

drivers

driver_idnamedate_of_birth
D1Jane Doe1996-03-14
D2Mark S1963-09-16
D3Adam L1965-05-19
D4Jaime L1976-05-19

trips

trip_iddriver_idtrip_datetrip_fare_dollarstrip_status
T1D12019-01-0117.52completed
T2D12019-01-024.40completed
T3D12019-01-03NULLcanceled
T4D22019-01-0125.00completed
T5D32019-01-018.00completed
T7D22019-01-02NULLcanceled

Write SQL (one query or two CTEs is fine) to: (a) Return driver names whose average fare over completed trips is strictly greater than 10.00. Exclude non-completed trips and rows where trip_fare_dollars IS NULL. Output columns: driver_name, avg_fare_2dp (rounded to 2 decimals). Order by avg_fare_2dp DESC, then driver_name ASC. Drivers with zero completed trips must not appear. (b) Count completed trips by driver age bands as of 2025-09-01. Use inclusive boundaries for bands: 20–35 and 36–45 (i.e., ages in [20,35] and [36,45]). Compute age from date_of_birth accurately (no 365-day approximation). Output two rows with columns: age_band ('20-35' or '36-45'), total_completed_trips. Ignore drivers outside 20–45. Edge cases to handle: null fares, canceled trips, drivers without completed trips, leap-year birthdays, and band boundary birthdays on 2025-09-01.

Drivers with average completed-trip fare > $10

You are given two tables: drivers and trips.

Write a SQL query to return driver names whose average fare over completed trips is strictly greater than 10.00.

Rules:

  • Only include trips where trip_status = 'completed'.
  • Exclude rows where trip_fare_dollars IS NULL.
  • Drivers with zero qualifying completed trips must not appear.

Output columns:

  • driver_name
  • avg_fare_2dp (average fare rounded to 2 decimals)

Sort by avg_fare_2dp DESC, then driver_name ASC.

Table drivers

columntypeconstraints
driver_idVARCHAR(10)PRIMARY KEY, NOT NULL
nameVARCHAR(100)NOT NULL
date_of_birthDATE

Sample data:

date_of_birthdriver_idname
1996-03-14D1Jane Doe
1963-09-16D2Mark S
1965-05-19D3Adam L
1976-05-19D4Jaime L
2004-02-29D5Lily Leap
1992-07-01D6Ten Avg
1991-01-15D7Null Fare
1993-11-10D8Aaron Z
1990-09-01D9Boundary35
1989-09-01D10Boundary36
1980-09-01D11Boundary45
2005-09-01D12Boundary20
1994-06-01D13Cancel Only
1979-09-01D14Outside46

Table trips

columntypeconstraints
trip_idVARCHAR(10)PRIMARY KEY, NOT NULL
driver_idVARCHAR(10)NOT NULL
trip_dateDATENOT NULL
trip_fare_dollarsDECIMAL(10,2)
trip_statusVARCHAR(20)NOT NULL

Sample data:

driver_idtrip_datetrip_fare_dollarstrip_idtrip_status
D12019-01-0117.52T1completed
D12019-01-024.4T2completed
D12019-01-03NULLT3canceled
D22019-01-0125T4completed
D32019-01-018T5completed
D22019-01-02NULLT6canceled
D62019-02-0110T7completed
D62019-02-0210T8completed
D72019-03-01NULLT9completed
D72019-03-0212T10canceled
D82019-04-0130T11completed
D82019-04-0220T12completed
D52025-01-05NULLT13completed
D102024-12-3115T14completed
D102025-01-015T15completed
D112025-05-200T16completed
D122025-02-157.5T17completed
D132025-03-109T18canceled

Expected output

One row per driver whose average fare across qualifying completed trips (excluding NULL fares) is > 10.00, with the average rounded to 2 decimals.

avg_fare_2dpdriver_name
25Aaron Z
25Mark S
10.96Jane Doe

Hints

  1. Filter to completed trips and non-NULL fares before aggregating.
  2. Use HAVING for the average-fare threshold after GROUP BY.

Completed trip counts by age band as of 2025-09-01

Using the same drivers and trips tables, count completed trips by driver age bands as of 2025-09-01.

Requirements:

  • Compute age from date_of_birth accurately (do not approximate using 365 days).
  • Use inclusive age bands: 20–35 and 36–45.
  • Ignore drivers outside ages 20–45.
  • Count trips where trip_status = 'completed' (fares may be NULL; still count the trip).
  • Output exactly two rows for the age bands, even if a band has 0 trips.

Output columns:

  • age_band (either '20-35' or '36-45')
  • total_completed_trips

Table drivers

columntypeconstraints
driver_idVARCHAR(10)PRIMARY KEY, NOT NULL
nameVARCHAR(100)NOT NULL
date_of_birthDATE

Sample data:

date_of_birthdriver_idname
1996-03-14D1Jane Doe
1963-09-16D2Mark S
1965-05-19D3Adam L
1976-05-19D4Jaime L
2004-02-29D5Lily Leap
1992-07-01D6Ten Avg
1991-01-15D7Null Fare
1993-11-10D8Aaron Z
1990-09-01D9Boundary35
1989-09-01D10Boundary36
1980-09-01D11Boundary45
2005-09-01D12Boundary20
1994-06-01D13Cancel Only
1979-09-01D14Outside46

Table trips

columntypeconstraints
trip_idVARCHAR(10)PRIMARY KEY, NOT NULL
driver_idVARCHAR(10)NOT NULL
trip_dateDATENOT NULL
trip_fare_dollarsDECIMAL(10,2)
trip_statusVARCHAR(20)NOT NULL

Sample data:

driver_idtrip_datetrip_fare_dollarstrip_idtrip_status
D12019-01-0117.52T1completed
D12019-01-024.4T2completed
D12019-01-03NULLT3canceled
D22019-01-0125T4completed
D32019-01-018T5completed
D22019-01-02NULLT6canceled
D62019-02-0110T7completed
D62019-02-0210T8completed
D72019-03-01NULLT9completed
D72019-03-0212T10canceled
D82019-04-0130T11completed
D82019-04-0220T12completed
D52025-01-05NULLT13completed
D102024-12-3115T14completed
D102025-01-015T15completed
D112025-05-200T16completed
D122025-02-157.5T17completed
D132025-03-109T18canceled

Expected output

Two rows (one per band) containing the total number of completed trips taken by drivers whose age on 2025-09-01 falls within that band.

age_bandtotal_completed_trips
20-359
36-453

Hints

  1. To compute accurate age in years, subtract 1 when the birthday in the reference year has not occurred yet.
  2. To guarantee both bands appear, build a two-row "bands" CTE and LEFT JOIN the computed counts.