Window function: running total over date
Last updated: August 25, 2025
Quick Overview
Use window functions to compute running total partitioned by user_id.
Redfin
Data Manipulation (SQL/Python)
Data Scientist
Redfin
August 25, 2025Data Scientist
Onsite
Data Manipulation (SQL/Python)
Easy
5
7
3,058 solved
Use window functions to compute running total partitioned by user_id.
Redfin asks this during the Onsite because data engineering skills are critical for the role. You should be comfortable with complex joins, window functions, CTEs, and performance optimization.
What the Interviewer Expects
- Write syntactically correct SQL with proper JOIN and WHERE clauses
- Use GROUP BY and aggregate functions appropriately
- Handle NULL values correctly in your queries
- Explain the query execution plan at a high level
Key Topics to Cover
Common Table Expressions (CTEs)
Data cleaning and transformation
Subqueries and correlated subqueries
Date/time manipulation
Window functions (ROW_NUMBER, RANK, LAG, LEAD)
Aggregate functions and GROUP BY
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- What would you do if this query needs to run every 5 minutes?
- How would you validate the correctness of your query results?
- How would you handle this if the data was spread across multiple databases?
- Can you rewrite this without using subqueries?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Problem Understanding
In this problem, we need to compute a running total of some financial metric (e.g., sales, transactions) for each user identified by user_id. The data likely comes from a transactions table which in...
Approach
To solve this problem, we will follow these steps:
- Identify the relevant data source, which is the transactions table.
- Use a Common Table Expression (CTE) to aggregate the data by
user_idand ...
Submit Your Answer
Markdown supported