Advanced SQL: Window Functions & CTEs for Complex Analysis Questions
Advanced SQL techniques using window functions (ROW_NUMBER, RANK, DENSE_RANK, etc.) and common table expressions (CTEs), including recursive queries, for complex data analysis, ranking and analytics patterns, cumulative totals, and multi-step data transformations within relational databases and data warehousing contexts.
HardTechnical
34 practiced
You inherit a parent-child category table for a product catalog. The business needs each category's full ancestor path, its depth in the hierarchy, and a safe rollup of sales to all ancestors. Some records are malformed and create cycles or orphan nodes. How would you query this with a recursive CTE while protecting the warehouse from runaway recursion?
HardTechnical
66 practiced
A marketing team has impression, click, and conversion tables with user_id and event_time. Design a SQL analysis that attributes each conversion to the first click within a 7-day lookback window, then reports conversion rate by campaign. Explain how you would structure the CTEs and how you would avoid double-attributing one conversion to multiple clicks.
MediumTechnical
65 practiced
Design a SQL query for cohort retention analysis using tables users(user_id, signup_date) and events(user_id, event_date). For each signup month, return the percentage of users who were active in month 0, month 1, and month 2 after signup. Use CTEs to make the logic easy to audit.
MediumTechnical
47 practiced
Given event_log(user_id, event_time, event_name), write a CTE-based SQL query that computes a three-step funnel: visited landing page, viewed product, purchased. Return step-level counts and conversion rates, and make sure each user is counted at most once per step in the correct chronological order.
HardTechnical
57 practiced
Given segment_scores(segment, user_id, score), write a query that returns the median score and the 90th percentile score for each segment. If your SQL dialect does not support a native percentile function, explain a window-function-based workaround and its trade-offs.
Unlock Full Question Bank
Get access to hundreds of Advanced SQL: Window Functions & CTEs for Complex Analysis interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.