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.

MediumTechnical
77 practiced

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?

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?

HardTechnical
70 practiced

Implement Type 2 slowly changing dimension logic: for each business key in a staging table, keep every historical version by effective_date but mark exactly one row per key as current_flag = true. Use a window function to determine which row is current and write the upsert into the dimension table.

HardTechnical
62 practiced

For a new KPI calculation that will be reused across multiple dashboards, decide between a CTE, a temporary/staging table, and a materialized view. What criteria drive the decision (readability, reuse, performance, indexability, freshness, transactional behavior), and how does your answer differ for: a one-off ad hoc analysis, a repeatedly-used expensive calculation, and a near-real-time dashboard?

MediumTechnical
69 practiced

You want to check whether related rows exist in another table without duplicating the left-hand rows the way a plain JOIN can. Using orders and payments, show when you'd reach for EXISTS instead of a JOIN specifically to avoid row duplication (not for the NULL-handling reason), with an example where the JOIN version silently multiplies rows.

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.