Zero2Dataengineer

Zero2Dataengineer

Break It. Build It.

πŸ”“ πŸ“’ Day 13/30 - DEEP DIVE SOLUTIONS: SQL, PYTHON, ETL, DATA MODELING

Solutions for March 12th, 2025 CHALLENGE – Unlock the Full Breakdown + Live Runnable Code! πŸš€

Avantika_Penumarty's avatar
Avantika_Penumarty
Mar 13, 2025
βˆ™ Paid

πŸ‘‹ 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.

This Substack is reader-supported. To receive new posts and support my work, consider becoming a free or paid subscriber.

UPGRADE TO ANNUAL


User's avatar

Continue reading this post for free, courtesy of Avantika_Penumarty.

Or purchase a paid subscription.
Β© 2026 Avantika Β· Privacy βˆ™ Terms βˆ™ Collection notice
Start your SubstackGet the app
Substack is the home for great culture