cswork / libraryOpen practice workspace

Coding questions · CloudTrucks

Calculate Average Rate and Cumulative Jobs by State

This question evaluates competency in relational data manipulation and analytical SQL concepts—including joins, aggregation with DISTINCT counts, per-date averaging, rolling cumulative counts, and handling numeric edge cases—within the Data Manipulation (SQL/Python) domain for a data scientist role.

jobs

+--------+---------+-----------+------------+----------------+-------+----------------+---------------+
| job_id | load_id | driver_id | status     | delivered_date | miles | payment_issued | rate_per_mile |
+--------+---------+-----------+------------+----------------+-------+----------------+---------------+
| 1      | 101     | 501       | completed  | 2023-07-01     | 450   | true           | 2.2           |
| 2      | 102     | 502       | cancelled  | 2023-07-03     | 0     | false          | 0             |
| 3      | 103     | 501       | completed  | 2023-07-04     | 600   | true           | 1.9           |
+--------+---------+-----------+------------+----------------+-------+----------------+---------------+

loads

+---------+--------------+-------------------+--------------+
| load_id | origin_state | destination_state | created_date |
+---------+--------------+-------------------+--------------+
| 101     | CA           | TX                | 2023-06-25   |
| 102     | NV           | AZ                | 2023-06-26   |
| 103     | OR           | WA                | 2023-06-27   |
+---------+--------------+-------------------+--------------+

drivers

+-----------+-----------+-----------+-------+
| driver_id | name      | hire_date | state |
+-----------+-----------+-----------+-------+
| 501       | Alice     | 2022-01-10| CA    |
| 502       | Bob       | 2021-11-05| NV    |
+-----------+-----------+-----------+-------+
Scenario

SQL screen on logistics data – tables jobs, loads, drivers for a trucking marketplace.

Question

Among the five states that appear most often in the loads table, which state has the highest count of DISTINCT completed jobs? For completed jobs, calculate the average rate_per_mile for each delivered_date (exclude rows where miles = 0). Produce a running cumulative count of completed jobs by delivered_date using a window function (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW).

Hints

Join jobs ↔ loads, filter status = 'completed', be careful with DISTINCT and division by zero, and use window functions correctly.

Top Load State Completed Jobs and Daily Metrics

You are given three tables for a trucking marketplace: jobs, loads, and drivers.

  1. Among the five states that appear most often in the loads table (considering both origin_state and destination_state), find the single state that has the highest count of DISTINCT completed jobs associated with it. If there is a tie, pick the alphabetically smallest state.

  2. For that winning state, and only for jobs with status = 'completed', calculate the following per delivered_date:

    • The average rate_per_mile, excluding rows where miles = 0 (these rows should not contribute to the average).
    • The number of completed jobs that day (COUNT of DISTINCT job_id for that day).
    • A running cumulative count of completed jobs over time, ordered by delivered_date, using a window function with ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.

Return one row per delivered_date for the winning state, including the state, delivered_date, avg_rate_per_mile, daily_completed_jobs, and cumulative_completed_jobs.

Table jobs

columntypeconstraints
job_idINTEGERPRIMARY KEY, NOT NULL
load_idINTEGERNOT NULL, FOREIGN KEY -> loads.load_id
driver_idINTEGERNOT NULL, FOREIGN KEY -> drivers.driver_id
statusVARCHAR(20)NOT NULL
delivered_dateDATE
milesINTEGERNOT NULL
payment_issuedBOOLEANNOT NULL
rate_per_mileDECIMAL(5,2)NOT NULL

Sample data:

delivered_datedriver_idjob_idload_idmilespayment_issuedrate_per_milestatus
2023-07-015011101450true2.2completed
2023-07-0350221020false0cancelled
2023-07-045013103600true1.9completed

Table loads

columntypeconstraints
load_idINTEGERPRIMARY KEY, NOT NULL
origin_stateCHAR(2)NOT NULL
destination_stateCHAR(2)NOT NULL
created_dateDATENOT NULL

Sample data:

created_datedestination_stateload_idorigin_state
2023-06-25TX101CA
2023-06-26AZ102NV
2023-06-27WA103OR

Table drivers

columntypeconstraints
driver_idINTEGERPRIMARY KEY, NOT NULL
nameVARCHAR(100)NOT NULL
hire_dateDATENOT NULL
stateCHAR(2)NOT NULL

Sample data:

driver_idhire_datenamestate
5012022-01-10AliceCA
5022021-11-05BobNV

Expected output

Return per-day metrics for the single 'winning' state: the state among the top-5 most frequent states in the loads table (considering both origin_state and destination_state) that has the highest DISTINCT completed job count; ties are broken alphabetically. For that state and for each delivered_date with completed jobs, show the state, the average rate_per_mile (excluding rows where miles = 0), the number of completed jobs that day (COUNT DISTINCT job_id), and the running cumulative count of completed jobs over time ordered by delivered_date.

avg_rate_per_milecumulative_completed_jobsdaily_completed_jobsdelivered_datestate
2.2112023-07-01CA

Hints

  1. Treat both origin_state and destination_state as contributing to state frequency and association with jobs (e.g., via an unpivot/UNION ALL).
  2. First find the top 5 most frequent states in loads, then restrict all later computations to those states.
  3. Join jobs to loads and filter status = 'completed' before aggregating.
  4. Use COUNT(DISTINCT job_id) when aggregating by state to avoid double-counting a job when origin_state = destination_state.
  5. Exclude rows with miles = 0 only from the AVG(rate_per_mile) calculation, not from the completed job counts.
  6. Compute the running total with a window function such as SUM(daily_completed_jobs) OVER (ORDER BY delivered_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW).