Reviewed by Aditya Kumar · Last reviewed 2026-08-08
To find the total number of rides per driver in the last 30 days, you need to filter the rides table by ride date and then aggregate the count of rides for each driver id . Mechanics and Why This…
This medium-level SQL question appears frequently in data engineering interviews at companies like Uber. While less common, it tests deeper understanding that distinguishes strong candidates.
Break this problem into components. Identify the core trade-offs involved, then walk the interviewer through your reasoning step by step. Demonstrate awareness of edge cases and production considerations - this is what separates good answers from great ones. The expert answer includes a code example that demonstrates the implementation pattern.
To find the total number of rides per driver in the last 30 days, you need to filter the rides table by ride_date and then aggregate the count of rides for each driver_id.
SELECT
driver_id,
COUNT(ride_id) AS ride_count -- Using ride_id for explicit count of distinct rides
FROM
rides
WHERE
ride_date >= CURRENT_DATE - INTERVAL '30' DAY -- Standard SQL for date subtraction
AND status = 'completed' -- Focus on completed rides for meaningful metrics
GROUP BY
driver_id
ORDER BY
ride_count DESC;
This query first filters the rides table to include only records from the last 30 days using ride_date >= CURRENT_DATE - INTERVAL '30' DAY. The INTERVAL syntax is standard SQL, though some databases might use DATE_SUB(CURRENT_DATE, INTERVAL 30 DAY) (MySQL) or CURRENT_DATE - 30 (PostgreSQL/Snowflake). We also add AND status = 'completed' to ensure we're counting actual, successful rides, which is crucial for most business metrics. After filtering, GROUP BY driver_id aggregates the results, and COUNT(ride_id) (or COUNT(*)) tallies the number of rides for each driver. Finally, ORDER BY ride_count DESC sorts the drivers by their ride volume.
In a production environment, consider the following:
* Data Type and Precision: Ensure ride_date is indexed and appropriately typed (e.g., DATE or TIMESTAMP). If it's a TIMESTAMP, you might need CAST(ride_timestamp AS DATE) or DATE(ride_timestamp) for accurate daily comparisons.
* Performance: For large tables, an index on (ride_date, driver_id, status) is critical for efficient filtering and grouping. If the rides table is partitioned by ride_date (common in data warehouses like Snowflake or Spark), the WHERE clause will enable partition pruning, significantly reducing the data scanned.
* Ride Status: Always clarify what constitutes a "ride." status = 'completed' is a common requirement to exclude cancelled, pending, or failed rides.
* Data Freshness: CURRENT_DATE (or GETDATE(), NOW()) reflects the execution time. If this query runs daily, it consistently provides a 30-day rolling window.
Discuss how this metric could be used (e.g., driver performance analysis, incentive programs, capacity planning) and the importance of data quality checks on driver_id and ride_date to ensure accurate results.
Red Flag: Counting cancelled rides when only completed matter. Pro-Move: 'We filter status=completed; partition by ride_date.'
Some links below are affiliate links. If you buy through them we may earn a small commission at no extra cost to you — it helps keep DataEngPrep free.
According to DataEngPrep.tech, this is one of the most frequently asked SQL interview questions, reported at 1 company. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.