InterviewStack.io LogoInterviewStack.io

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.

HardTechnical
69 practiced

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?

HardSystem Design
70 practiced

A 12-week retention matrix (one row per signup cohort, one column per week offset, showing percent still active) needs to run nightly against a table of hundreds of millions of users. Beyond just writing the CTE-and-window-function pipeline, propose the performance strategy that makes this feasible: pre-aggregation, partitioning, materialization, or sampling. Then address a related wrinkle: cohort assignment sometimes requires two sequential events (say signup and onboarding-completed) rather than a single timestamp, and events can arrive late and need backfilling without recomputing the whole table.

MediumTechnical
77 practiced

Compare a CTE, a derived-table subquery, and a temporary table along one specific axis: scope and persistence. If two separate statements in the same session both need the intermediate result, or if you need it to survive across a transaction boundary, which of the three actually lets you do that, and why do the other two not?

MediumTechnical
62 practiced

Given an employees table with employee_id, manager_id, and name, write a recursive CTE that returns each employee's full reporting chain up to the top, as a path string like 'CEO > VP > Manager > Employee' along with the depth. Cap the traversal at a reasonable max depth and make sure a bad manager_id cycle in the data can't send it into an infinite loop.

HardTechnical
67 practiced

Compute cohort-based lifetime value: for each acquisition cohort (say signup month), the cumulative revenue per cohort at day/week/month offsets 0, 1, 2, and so on. Handle sparse cohorts (small cohorts with missing weeks) and, if the business operates in multiple currencies, converting each transaction to a common currency using the exchange rate in effect on that date. Discuss how you'd keep this scalable rather than running a heavy per-user window calculation over each user's entire lifetime.

Unlock Full Question Bank

Get access to all Advanced SQL: Window Functions, CTEs, and Subqueries interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.