Join trips to payments and label each trip as Unpaid, Paid or Paid with Tip

Quick Overview

Write a SQL query that lists every trip with its fare, tip and total payment, filling zeros for trips that have no payment and labeling each trip Unpaid, Paid or Paid with Tip. It tests LEFT JOIN behavior, COALESCE on unmatched rows, and CASE logic that checks whether a joined row exists.

Join trips to payments and label each trip as Unpaid, Paid or Paid with Tip

Company: Uber

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

A ride service stores trips in `trips` and keeps payment details in a separate `payments` table linked by `trip_id`. A trip may not have been paid yet, so it can have no payment row. Return every trip with its payment details, plus `total_payment` (`base_fare + tip_amount`) and `payment_status`. Order by `trip_id` ascending. Requirements: - Columns: `trip_id, rider_name, driver_name, trip_date, distance_km, base_fare, tip_amount, total_payment, payment_status`. - Trips with no payment show `0` for all monetary fields. - `'Unpaid'`: no payment record. - `'Paid with Tip'`: payment exists and `tip_amount > 0`. - `'Paid'`: payment exists and `tip_amount` is 0. ```hint Detecting a missing payment Think about which column tells you reliably that a payment row exists for a trip, before any zeros are filled in. ``` ### Input Table(s) `trips` (primary key `trip_id`) | column | type | meaning | |---|---|---| | trip_id | INTEGER | Trip identifier | | rider_name | VARCHAR | Name of the rider | | driver_name | VARCHAR | Name of the driver | | trip_date | DATE | Date of the trip | | distance_km | NUMERIC(8,2) | Trip distance in kilometers | `payments` (primary key `payment_id`; `trip_id` is unique and references `trips.trip_id`) | column | type | meaning | |---|---|---| | payment_id | INTEGER | Payment identifier | | trip_id | INTEGER | Trip this payment belongs to | | base_fare | NUMERIC(10,2) | Fare charged for the trip | | tip_amount | NUMERIC(10,2) | Tip added to the fare, default 0 | ### Output Contract One row per trip in `trips`, paid or not, with these columns in this order: | column | definition | |---|---| | trip_id | From `trips` | | rider_name | From `trips` | | driver_name | From `trips` | | trip_date | From `trips` | | distance_km | From `trips` | | base_fare | `payments.base_fare`, or 0 when the trip has no payment | | tip_amount | `payments.tip_amount`, or 0 when the trip has no payment | | total_payment | `base_fare + tip_amount` after the zeros are filled in | | payment_status | `'Unpaid'`, `'Paid'` or `'Paid with Tip'`, as defined above | Rows are ordered by `trip_id` ascending, which is unique. No rounding is applied. Amounts that come from `payments` keep their two decimal places. The zeros filled in for an unpaid trip may print as `0` instead of `0.00` (PostgreSQL prints `COALESCE` of a `NUMERIC(10,2)` and the integer 0 that way), so the monetary columns are compared as numbers, not as text. ### Example `trips` | trip_id | rider_name | driver_name | trip_date | distance_km | |---|---|---|---|---| | 1 | Alice | Raj | 2024-03-01 | 5.20 | | 2 | Ben | Mia | 2024-03-01 | 12.00 | | 3 | Chloe | Raj | 2024-03-02 | 3.40 | | 4 | Dan | Omar | 2024-03-03 | 8.75 | `payments` | payment_id | trip_id | base_fare | tip_amount | |---|---|---|---| | 501 | 1 | 11.50 | 2.00 | | 502 | 2 | 24.00 | 0.00 | | 503 | 4 | 18.25 | 3.50 | Expected result: | trip_id | rider_name | driver_name | trip_date | distance_km | base_fare | tip_amount | total_payment | payment_status | |---|---|---|---|---|---|---|---|---| | 1 | Alice | Raj | 2024-03-01 | 5.20 | 11.50 | 2.00 | 13.50 | Paid with Tip | | 2 | Ben | Mia | 2024-03-01 | 12.00 | 24.00 | 0.00 | 24.00 | Paid | | 3 | Chloe | Raj | 2024-03-02 | 3.40 | 0 | 0 | 0 | Unpaid | | 4 | Dan | Omar | 2024-03-03 | 8.75 | 18.25 | 3.50 | 21.75 | Paid with Tip | Trip 3 has no payment row, so it still appears, with 0 in every monetary column and status `'Unpaid'`. Trip 2 has a payment with a zero tip, so it is `'Paid'`. Trips 1 and 4 have positive tips, and their totals are `11.50 + 2.00 = 13.50` and `18.25 + 3.50 = 21.75`. ### Constraints and Clarifications - At most 10,000 trips. - Each trip has at most one payment record, and each `payments.trip_id` references exactly one trip. - `tip_amount` defaults to 0 and is updated in place when a tip is added, so a trip never gets a second payment row. - `distance_km` is positive. `base_fare` and `tip_amount` are non-negative and never NULL in `payments`, and no column of `trips` is NULL. - Whether a payment row exists decides between `'Unpaid'` and the other two labels. A payment row with `tip_amount = 0` is `'Paid'` even if its `base_fare` is also 0. - Rider and driver names can repeat across trips; `trip_id` is the only unique key.

Overview: Write a SQL query that lists every trip with its fare, tip and total payment, filling zeros for trips that have no payment and labeling each trip Unpaid, Paid or Paid with Tip. It tests LEFT JOIN behavior, COALESCE on unmatched rows, and CASE logic that checks whether a joined row exists.

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

A ride service stores trips in trips and keeps payment details in a separate payments table linked by trip_id. A trip may not have been paid yet, so it can have no payment row.

Return every trip with its payment details, plus total_payment (base_fare + tip_amount) and payment_status. Order by trip_id ascending.

Requirements:

  • Columns: trip_id, rider_name, driver_name, trip_date, distance_km, base_fare, tip_amount, total_payment, payment_status .
  • Trips with no payment show 0 for all monetary fields.
  • 'Unpaid' : no payment record.
  • 'Paid with Tip' : payment exists and tip_amount > 0 .
  • 'Paid' : payment exists and tip_amount is 0.

Input Table(s)

trips (primary key trip_id)

columntypemeaning
trip_idINTEGERTrip identifier
rider_nameVARCHARName of the rider
driver_nameVARCHARName of the driver
trip_dateDATEDate of the trip
distance_kmNUMERIC(8,2)Trip distance in kilometers

payments (primary key payment_id; trip_id is unique and references trips.trip_id)

columntypemeaning
payment_idINTEGERPayment identifier
trip_idINTEGERTrip this payment belongs to
base_fareNUMERIC(10,2)Fare charged for the trip
tip_amountNUMERIC(10,2)Tip added to the fare, default 0

Output Contract

One row per trip in trips, paid or not, with these columns in this order:

columndefinition
trip_idFrom trips
rider_nameFrom trips
driver_nameFrom trips
trip_dateFrom trips
distance_kmFrom trips
base_farepayments.base_fare, or 0 when the trip has no payment
tip_amountpayments.tip_amount, or 0 when the trip has no payment
total_paymentbase_fare + tip_amount after the zeros are filled in
payment_status'Unpaid', 'Paid' or 'Paid with Tip', as defined above

Rows are ordered by trip_id ascending, which is unique. No rounding is applied. Amounts that come from payments keep their two decimal places. The zeros filled in for an unpaid trip may print as 0 instead of 0.00 (PostgreSQL prints COALESCE of a NUMERIC(10,2) and the integer 0 that way), so the monetary columns are compared as numbers, not as text.

Example

trips

trip_idrider_namedriver_nametrip_datedistance_km
1AliceRaj2024-03-015.20
2BenMia2024-03-0112.00
3ChloeRaj2024-03-023.40
4DanOmar2024-03-038.75

payments

payment_idtrip_idbase_faretip_amount
501111.502.00
502224.000.00
503418.253.50

Expected result:

trip_idrider_namedriver_nametrip_datedistance_kmbase_faretip_amounttotal_paymentpayment_status
1AliceRaj2024-03-015.2011.502.0013.50Paid with Tip
2BenMia2024-03-0112.0024.000.0024.00Paid
3ChloeRaj2024-03-023.40000Unpaid
4DanOmar2024-03-038.7518.253.5021.75Paid with Tip

Trip 3 has no payment row, so it still appears, with 0 in every monetary column and status 'Unpaid'. Trip 2 has a payment with a zero tip, so it is 'Paid'. Trips 1 and 4 have positive tips, and their totals are 11.50 + 2.00 = 13.50 and 18.25 + 3.50 = 21.75.

Constraints and Clarifications

  • At most 10,000 trips.
  • Each trip has at most one payment record, and each payments.trip_id references exactly one trip.
  • tip_amount defaults to 0 and is updated in place when a tip is added, so a trip never gets a second payment row.
  • distance_km is positive. base_fare and tip_amount are non-negative and never NULL in payments , and no column of trips is NULL.
  • Whether a payment row exists decides between 'Unpaid' and the other two labels. A payment row with tip_amount = 0 is 'Paid' even if its base_fare is also 0.
  • Rider and driver names can repeat across trips; trip_id is the only unique key.