Essential cookies keep authentication working. With your permission, we also use analytics cookies to understand and improve the product. Read our Privacy Policy

DataEngPrep.tech
QuestionsPracticeAI CoachDashboardPricingBlog
ProLogin
Home/Questions/SQL/Write a query to find the total number of rides per driver in the last 30 days.

Write a query to find the total number of rides per driver in the last 30 days.

SQLmedium2 min read

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…

🤖 Analyze Your Answer
Frequency
Low
Asked at 1 company
Category
487
questions in SQL
Difficulty Split
130E|271M|86H
in this category
Total Bank
1,863
across 7 categories
Asked at these companies
Uber

Why This Question Matters

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.

How to Approach This

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.

Expert Answer
358 wordsIncludes code

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;

Mechanics and Why

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.

Production Considerations and Trade-offs

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.

In the interview, also mention…

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.

⚡
Pro Tip

Red Flag: Counting cancelled rides when only completed matter. Pro-Move: 'We filter status=completed; partition by ride_date.'

Want all answers as a PDF for offline study?
Seven focused volumes with 750+ in-depth answers — Answer Vault →

Related SQL Questions

mediumWrite an SQL query to find the second-highest salary from an employee table.FreemediumDemonstrate the difference between DENSE_RANK() and RANK()FreemediumDiscuss differences between ROW_NUMBER(), RANK(), and DENSE_RANK(), and provide examples from your projects.FreemediumExplain the differences between Data Warehouse, Data Lake, and Delta LakeFreemediumExplain the differences between Repartition and Coalesce. When would you use each?Free

Level up your prep

Recommended
Educative
Educative Unlimited

800+ hands-on courses — Grokking System Design, Coding Patterns, and AI mock interviews for your DE loop.

Start learning →

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.

← Back to all questionsMore SQL questions →
Categories
All QuestionsSQLSpark / Big DataPython / CodingSystem DesignCloud / ToolsBehavioral
By Company
AmazonGoogleDatabricksSnowflakeAWSAzureMicrosoftNetflixUberTCS
Interview Guides
All GuidesTop SQL QuestionsTop Spark QuestionsPySpark QuestionsTop Python QuestionsTop System DesignKafka QuestionsAirflow QuestionsSQL Window FunctionsETL QuestionsData Modeling
Products
AI Interview CoachAnswer AnalyzerSQL PlaygroundResume AnalyzerAnswer Vault PDFsPricing
Company
About & Editorial PolicyContact UsAI DisclosureDisclaimerTerms of ServicePrivacy Policy
© 2026 DataEngPrep.tech. All rights reserved.
AboutBlogContactDisclaimer