π π’ Day 13/30 - DEEP DIVE SOLUTIONS: SQL, PYTHON, ETL, DATA MODELING
Solutions for March 12th, 2025 CHALLENGE β Unlock the Full Breakdown + Live Runnable Code! π
π Hey Data Engineers!
Welcome to Day 13 of the 30-Day Data Engineering Challenge! π
Todayβs Deep Dive covers:
β
SQL Recursive Queries (Building Hierarchical Queries with CTEs)
β
Python Generators vs. List Comprehensions (Efficient Memory Usage)
β
ETL Job Scheduling with Apache Airflow (Automating ETL Pipelines)
β
Data Warehouse Performance Optimization (Partitioning for Faster Queries)
π§ Master these concepts with:
β
Clear explanations & reasoning
β
Live runnable code & breakdowns
β
Real-world best practices
π Unlock the full deep dive & runnable code by upgrading to the Annual Plan!
π SQL Challenge - Recursive CTEs
Understanding Recursive Queries in SQL
A recursive common table expression (CTE) allows a query to reference itself, enabling hierarchical data traversal (e.g., organizational structures, category trees, dependency graphs).
πΉ Where Itβs Used in Real-World Applications?
β
Employee Hierarchies β Finding reporting relationships in companies.
β
Bill of Materials (BOM) β Identifying dependencies in manufacturing.
β
Graph Queries β Traversing connected data like social networks.
Run & Test on onecompiler.com
1οΈβ£ Open onecompiler.com, select SQL (PostgreSQL or MySQL).
2οΈβ£ Copy and paste the SQL query below:
-- Create Employee Table
CREATE TABLE employees (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
manager_id INT
);
-- Insert Sample Data
INSERT INTO employees VALUES
(1, 'Alice', NULL),
(2, 'Bob', 1),
(3, 'Charlie', 1),
(4, 'David', 2),
(5, 'Eve', 2),
(6, 'Frank', 3);
-- Recursive Query to Find Employee Hierarchy
WITH RECURSIVE employee_hierarchy AS (
SELECT emp_id, emp_name, manager_id, 1 AS level
FROM employees
WHERE manager_id IS NULL
UNION ALL
SELECT e.emp_id, e.emp_name, e.manager_id, h.level + 1
FROM employees e
JOIN employee_hierarchy h ON e.manager_id = h.emp_id
)
SELECT * FROM employee_hierarchy;3οΈβ£ Click Run and analyze the output.
Whatβs Happening in the Output?
β Recursively retrieves employee hierarchy levels from the CEO down to employees.
β Uses UNION ALL to iteratively build relationships between managers and employees.
πΉ Optimizations & Best Practices
β
Always include a base case (WHERE manager_id IS NULL) to prevent infinite loops.
β
Use indexing on hierarchical keys (e.g., manager_id) for efficient lookups.
β
Limit depth with OPTION (MAXRECURSION 5) in SQL Server to avoid excessive recursion.




