Level up your SQL skills with advanced patterns used in real-world data pipelines and technical interviews from window functions and time-series analysis to deduplication, recursive queries, pivoting, and transaction management. This chapter focuses on solving problems that require more than basic filtering and aggregation. Each topic includes a practical example, an explanation of the logic, and interview tips to help you understand when and why to use it. In this chapter Sample dataset Window functions: ROW_NUMBER, RANK, DENSE_RANK LAG and LEAD Running totals and moving averages Deduplication Top N per group Gaps and islands Recursive CTEs Pivoting and unpivoting Stored procedures and transactions Interview practice Sample dataset used in this chapter We'll use an employee_sales table to demonstrate ranking and time-series analysis. employee_id employee_name department sale_date sales 1 Alice Sales 2026-01-01 100 1 Alice Sales 2026-01-02 150 1 Alice ...
Take your SQL skills beyond the basics with practical examples covering joins, subqueries, CTEs, set operations, date and string functions, conditional aggregation, and filtering with EXISTS or IN. These patterns are used every day in data pipelines, warehouse transformations, analytics models, and technical interviews. Work through the examples and try the practice questions to build confidence. In this chapter Sample datasets INNER, LEFT, RIGHT and FULL JOIN Subqueries and Common Table Expressions (CTEs) UNION vs. UNION ALL Date and string functions Conditional aggregation EXISTS vs. IN Interview practice Sample datasets used in this chapter We’ll use two small tables: customers and orders . customers customer_id customer_name city 1 Alice Bangalore 2 Bob Mumbai 3 Charlie Delhi 4 Diana Pune orders order_id customer_id amount order_date status 101 1 1200 2026-01-05 Completed 102 1 800 2026-01-15 Pending 103 2 2000 2026-02-02 Completed 104 5 500 2026-02-1...