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
78 practiced

You're computing a rolling sum over the last 7 readings for a sensor, but readings arrive at irregular intervals (sometimes seconds apart, sometimes hours). Explain why RANGE BETWEEN on the timestamp column doesn't do what most people expect here, and what the robust alternative is.

HardTechnical
80 practiced

At a high level, how do database engines actually execute a window function: does it require a sort, how does partition-wise processing work, and what shows up in an EXPLAIN plan when a window operation is expensive (memory usage, spill to disk)? Also touch on how this changes in a distributed engine (BigQuery, Snowflake, Redshift) where the partition/order columns drive a shuffle.

HardTechnical
63 practiced

Two large tables A and B: you need to check whether a row in A has any matching row in B, without duplicating A's rows and without a huge WHERE id IN (subquery) blowing up. Compare EXISTS/NOT EXISTS against LEFT JOIN ... IS NULL for this, and discuss how duplicates in B and indexing choices change which one is actually faster.

MediumTechnical
78 practiced

You're building analytical features or reports and need row-level detail preserved alongside a group-level aggregate. Give three concrete situations where a window function is the right tool instead of a GROUP BY aggregate, and for each one explain specifically what row-level information a GROUP BY would have thrown away.

MediumTechnical
118 practiced

Given a table of user events with a timestamp, write a query that assigns a session_id per user, where a new session starts whenever the gap since the previous event for that user exceeds 30 minutes. Return session_start, session_end, and event_count per session, and explain how your approach would need to change for events crossing device boundaries or spanning multiple time zones.

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.