Advanced SQL: Window Functions, CTEs, and Subqueries Questions
Analytical SQL for complex problems: window functions (ranking, running totals, LAG/LEAD, partitioned aggregates), common table expressions including recursive CTEs, and scalar, nested, and correlated subqueries. Covers when each construct is the right tool and how they compose for multi-step analysis. The differentiator between basic and senior SQL competence.
Finance wants a month-to-date revenue trend by product from a daily sales fact table, but some product-day combinations are missing because there were no sales. The report still needs to show zero-revenue days and reset correctly at each month boundary. How would you structure the query and what reference data, if any, would you need?
An executive dashboard needs the top 3 products by revenue in each region. If multiple products tie at the cutoff, every tied product must appear, but revenue should be computed from raw line items without double counting order-level facts. How would you build the query so the aggregation and ranking both stay correct?
An app team wants to find each user's longest streak of consecutive active days, and also return the start date, end date, and streak length. Multiple events can occur on the same day, and some users have gaps caused by missing source data. How would you solve this in SQL, and how would you guard against counting bad data as broken streaks?
A product team wants session-level engagement metrics from raw clickstream events. A session should end after 30 minutes of inactivity, and the same user may generate events from multiple devices. How would you define session boundaries in SQL and compute per-session metrics in a way that is robust to duplicate or out-of-order events?
A finance team wants monthly customer revenue reported against the customer segment that was valid when each order happened, not the segment the customer has today. Some dimension updates arrive late, and some orders are backfilled after the month closes. How would you structure the SQL transformation so the numbers are reproducible, auditable, and easy to reconcile?
Unlock Full Question Bank
Get access to all 10 Advanced SQL: Window Functions, CTEs, and Subqueries interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.