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?

EasyTechnical
76 practiced

Your orders table can contain multiple rows per customer because of updates and replays. A downstream report needs exactly one row per customer: the most recent order, and if two rows share the same timestamp the choice must be deterministic. How would you write the SQL, and how would you adjust it if the business later asks to keep every row tied for the latest timestamp?

HardTechnical
79 practiced

A correlated subquery in a WHERE clause is quietly returning wrong totals because the table it correlates against has duplicate rows for the join key. Walk through why this happens and show two ways to fix it: DISTINCT inside the subquery, and rewriting as a GROUP BY plus JOIN.

MediumTechnical
59 practiced

Explain the difference between PERCENT_RANK() and CUME_DIST(): what each formula computes and how their outputs differ on the same dataset, especially with ties. Then use one of them to compute each user's spend percentile within their own country, and say which of the two functions is the right one for that specific comparison and why.

MediumTechnical
59 practiced

Use NTILE to bucket users into deciles by spend. Explain what happens when the group size isn't evenly divisible by the number of buckets, and what happens on a small partition where NTILE can produce uneven or even empty-looking buckets. When would you reach for PERCENT_RANK or CUME_DIST instead of NTILE for a similar bucketing goal?

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.