Compute the charge and payment status for every parking garage reservation in SQL

Quick Overview

Write a SQL query that lists every parking garage reservation with its spot details, duration in minutes, expected charge, amount paid and payment status. It tests outer joins across three tables, timestamp arithmetic, rounding before comparison, and handling reservations with no payment.

Compute the charge and payment status for every parking garage reservation in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

A parking garage stores its spots, the reservations made on them, and the payments it received. Write a query that reports every reservation with its spot details, how many minutes it lasted, what it should have cost, how much was paid, and whether the payment covered the charge. ### Input Table(s) Table: `spots` | Column | Type | Meaning | |---|---|---| | spot_id | INT | Spot identifier | | floor_level | INT | Floor the spot is on | | spot_number | INT | Number shown on the spot | | spot_type | VARCHAR | One of 'compact', 'regular', 'oversized', 'handicap' | | hourly_rate | DECIMAL(6,2) | Price per hour, always positive | Primary key: `spot_id`. Table: `reservations` | Column | Type | Meaning | |---|---|---| | reservation_id | INT | Reservation identifier | | spot_id | INT | Reserved spot, references `spots.spot_id` | | start_time | TIMESTAMP | When the reservation starts | | end_time | TIMESTAMP | When it ends, strictly after `start_time` | Primary key: `reservation_id`. Table: `transactions` | Column | Type | Meaning | |---|---|---| | transaction_id | INT | Payment identifier | | reservation_id | INT | Reservation the payment is for, references `reservations.reservation_id` | | amount | DECIMAL(8,2) | Amount paid, never NULL | Primary key: `transaction_id`. `reservation_id` is unique in this table, so a reservation has at most one transaction. ### Output Contract One row per reservation, with these columns in this order: | Column | Meaning | |---|---| | reservation_id | The reservation | | floor_level | From the reserved spot | | spot_number | From the reserved spot | | spot_type | From the reserved spot | | hourly_rate | From the reserved spot | | start_time | Reservation start | | end_time | Reservation end | | duration_minutes | Whole minutes from `start_time` to `end_time` (integer) | | expected_charge | `hourly_rate * duration_minutes / 60`, rounded to 2 decimals | | amount_paid | The transaction's `amount`, or 0 when the reservation has no transaction, shown with 2 decimals | | payment_status | 'Unpaid', 'Paid' or 'Underpaid' (rules below) | - `payment_status` is 'Unpaid' when the reservation has no transaction. Otherwise it is 'Paid' when `amount_paid >= expected_charge` and 'Underpaid' when `amount_paid < expected_charge`, comparing against the rounded `expected_charge`. - Order by `floor_level`, `spot_number`, `start_time`, `reservation_id`, all ascending. `reservation_id` is unique, so the order is fully determined. ### Example Input `spots`: | spot_id | floor_level | spot_number | spot_type | hourly_rate | |---|---|---|---|---| | 1 | 1 | 101 | compact | 4.00 | | 2 | 1 | 102 | regular | 5.50 | | 3 | 2 | 201 | oversized | 8.00 | | 4 | 1 | 103 | handicap | 3.00 | Input `reservations`: | reservation_id | spot_id | start_time | end_time | |---|---|---|---| | 10 | 2 | 2024-05-01 09:00:00 | 2024-05-01 10:30:00 | | 11 | 1 | 2024-05-01 13:00:00 | 2024-05-01 13:40:00 | | 12 | 3 | 2024-05-01 07:00:00 | 2024-05-01 09:20:00 | | 13 | 1 | 2024-05-01 08:00:00 | 2024-05-01 10:00:00 | | 14 | 2 | 2024-05-01 11:00:00 | 2024-05-01 11:25:00 | Input `transactions`: | transaction_id | reservation_id | amount | |---|---|---| | 100 | 10 | 8.25 | | 101 | 11 | 2.50 | | 102 | 13 | 10.00 | | 103 | 14 | 2.29 | Expected output: | reservation_id | floor_level | spot_number | spot_type | hourly_rate | start_time | end_time | duration_minutes | expected_charge | amount_paid | payment_status | |---|---|---|---|---|---|---|---|---|---|---| | 13 | 1 | 101 | compact | 4.00 | 2024-05-01 08:00:00 | 2024-05-01 10:00:00 | 120 | 8.00 | 10.00 | Paid | | 11 | 1 | 101 | compact | 4.00 | 2024-05-01 13:00:00 | 2024-05-01 13:40:00 | 40 | 2.67 | 2.50 | Underpaid | | 10 | 1 | 102 | regular | 5.50 | 2024-05-01 09:00:00 | 2024-05-01 10:30:00 | 90 | 8.25 | 8.25 | Paid | | 14 | 1 | 102 | regular | 5.50 | 2024-05-01 11:00:00 | 2024-05-01 11:25:00 | 25 | 2.29 | 2.29 | Paid | | 12 | 2 | 201 | oversized | 8.00 | 2024-05-01 07:00:00 | 2024-05-01 09:20:00 | 140 | 18.67 | 0.00 | Unpaid | Reservation 13 overpaid (10.00 against 8.00), which still counts as 'Paid'. Reservation 11 should cost 4.00 times 40 minutes over 60, or 2.67 after rounding, and 2.50 falls short. Reservation 14 costs 2.2917 before rounding and 2.29 after, so a payment of 2.29 is 'Paid'. Reservation 12 has no transaction, so it shows 0.00 and 'Unpaid'. Spot 103 has no reservations and does not appear. Reservation 12 starts earliest but sorts last because it is on floor 2. ### Constraints and Clarifications - Every reservation's `spot_id` exists in `spots`. Spots with no reservations produce no rows. - 'Unpaid' depends only on whether a transaction exists. A transaction with `amount` 0.00 still counts as a payment and is compared with the expected charge like any other. - Timestamps are recorded on whole minutes, so `duration_minutes` is exact. A reservation can span midnight or several days. - `spots` has at most 100 rows; `reservations` and `transactions` have at most 10,000 rows each. ```hint Watch the arithmetic Check how your database divides one integer by another before you write the charge formula. ```

Overview: Write a SQL query that lists every parking garage reservation with its spot details, duration in minutes, expected charge, amount paid and payment status. It tests outer joins across three tables, timestamp arithmetic, rounding before comparison, and handling reservations with no payment.

General logo
General
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

A parking garage stores its spots, the reservations made on them, and the payments it received. Write a query that reports every reservation with its spot details, how many minutes it lasted, what it should have cost, how much was paid, and whether the payment covered the charge.

Input Table(s)

Table: spots

ColumnTypeMeaning
spot_idINTSpot identifier
floor_levelINTFloor the spot is on
spot_numberINTNumber shown on the spot
spot_typeVARCHAROne of 'compact', 'regular', 'oversized', 'handicap'
hourly_rateDECIMAL(6,2)Price per hour, always positive

Primary key: spot_id.

Table: reservations

ColumnTypeMeaning
reservation_idINTReservation identifier
spot_idINTReserved spot, references spots.spot_id
start_timeTIMESTAMPWhen the reservation starts
end_timeTIMESTAMPWhen it ends, strictly after start_time

Primary key: reservation_id.

Table: transactions

ColumnTypeMeaning
transaction_idINTPayment identifier
reservation_idINTReservation the payment is for, references reservations.reservation_id
amountDECIMAL(8,2)Amount paid, never NULL

Primary key: transaction_id. reservation_id is unique in this table, so a reservation has at most one transaction.

Output Contract

One row per reservation, with these columns in this order:

ColumnMeaning
reservation_idThe reservation
floor_levelFrom the reserved spot
spot_numberFrom the reserved spot
spot_typeFrom the reserved spot
hourly_rateFrom the reserved spot
start_timeReservation start
end_timeReservation end
duration_minutesWhole minutes from start_time to end_time (integer)
expected_chargehourly_rate * duration_minutes / 60, rounded to 2 decimals
amount_paidThe transaction's amount, or 0 when the reservation has no transaction, shown with 2 decimals
payment_status'Unpaid', 'Paid' or 'Underpaid' (rules below)
  • payment_status is 'Unpaid' when the reservation has no transaction. Otherwise it is 'Paid' when amount_paid >= expected_charge and 'Underpaid' when amount_paid < expected_charge , comparing against the rounded expected_charge .
  • Order by floor_level , spot_number , start_time , reservation_id , all ascending. reservation_id is unique, so the order is fully determined.

Example

Input spots:

spot_idfloor_levelspot_numberspot_typehourly_rate
11101compact4.00
21102regular5.50
32201oversized8.00
41103handicap3.00

Input reservations:

reservation_idspot_idstart_timeend_time
1022024-05-01 09:00:002024-05-01 10:30:00
1112024-05-01 13:00:002024-05-01 13:40:00
1232024-05-01 07:00:002024-05-01 09:20:00
1312024-05-01 08:00:002024-05-01 10:00:00
1422024-05-01 11:00:002024-05-01 11:25:00

Input transactions:

transaction_idreservation_idamount
100108.25
101112.50
1021310.00
103142.29

Expected output:

reservation_idfloor_levelspot_numberspot_typehourly_ratestart_timeend_timeduration_minutesexpected_chargeamount_paidpayment_status
131101compact4.002024-05-01 08:00:002024-05-01 10:00:001208.0010.00Paid
111101compact4.002024-05-01 13:00:002024-05-01 13:40:00402.672.50Underpaid
101102regular5.502024-05-01 09:00:002024-05-01 10:30:00908.258.25Paid
141102regular5.502024-05-01 11:00:002024-05-01 11:25:00252.292.29Paid
122201oversized8.002024-05-01 07:00:002024-05-01 09:20:0014018.670.00Unpaid

Reservation 13 overpaid (10.00 against 8.00), which still counts as 'Paid'. Reservation 11 should cost 4.00 times 40 minutes over 60, or 2.67 after rounding, and 2.50 falls short. Reservation 14 costs 2.2917 before rounding and 2.29 after, so a payment of 2.29 is 'Paid'. Reservation 12 has no transaction, so it shows 0.00 and 'Unpaid'. Spot 103 has no reservations and does not appear. Reservation 12 starts earliest but sorts last because it is on floor 2.

Constraints and Clarifications

  • Every reservation's spot_id exists in spots . Spots with no reservations produce no rows.
  • 'Unpaid' depends only on whether a transaction exists. A transaction with amount 0.00 still counts as a payment and is compared with the expected charge like any other.
  • Timestamps are recorded on whole minutes, so duration_minutes is exact. A reservation can span midnight or several days.
  • spots has at most 100 rows; reservations and transactions have at most 10,000 rows each.