Zero2Dataengineer

Zero2Dataengineer

Break It. Build It.

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

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

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

πŸ‘‹ Hey Data Engineers!

Welcome to Day 10 of the 30-Day Data Engineering Challenge πŸš€
Today’s Deep Dive covers:
βœ… SQL Pivoting (Transforming Rows to Columns)
βœ… Python Decorators (Modifying Function Behavior)
βœ… Data Lake vs. Data Warehouse (Understanding Storage Differences)
βœ… Entity-Relationship Diagrams (Modeling Database Relationships)

🧠 Don’t just memorizeβ€”understand. Every challenge solution includes:
βœ… Clear explanation & reasoning
βœ… Why this solution works
βœ… Key optimizations & best practices

Want deep dives + runnable code? Upgrade to the annual plan and master these concepts like a pro!

UPGRADE TO ANNUAL MEMBERSHIP


πŸ“Œ SQL Challenge - Mastering Pivoting Data

πŸ‘‰ Question: Which SQL clause is used to pivot data from rows to columns?

πŸ”˜ A) GROUP BY
πŸ”˜ B) PIVOT
πŸ”˜ C) TRANSFORM
πŸ”˜ D) JOIN

βœ… Answer: B) PIVOT

πŸ“– Explanation:
The PIVOT clause in SQL is used to transform row values into columns, commonly used for reporting and analytics. This is helpful for aggregating data and restructuring large datasets for readability.

πŸ”Ή Where It’s Used in Real-World Applications?
βœ… Sales Reports: Converting monthly sales data from rows into columns.
βœ… Survey Analysis: Pivoting categorical survey responses into separate columns.
βœ… Data Warehousing: Preparing analytical data in wide-table formats.


Run & Test on onecompiler.com

1️⃣ Open onecompiler.com, select SQL (PostgreSQL or MySQL).
2️⃣ Copy and paste the SQL query below:

-- Sample sales data
CREATE TABLE sales (
    sale_id INT PRIMARY KEY,
    product VARCHAR(50),
    sale_month VARCHAR(10),
    amount DECIMAL(10,2)
);

INSERT INTO sales VALUES
(1, 'Laptop', 'January', 1000.00),
(2, 'Laptop', 'February', 1200.00),
(3, 'Phone', 'January', 800.00),
(4, 'Phone', 'February', 900.00);

-- Pivoting monthly sales data
SELECT product,
    SUM(CASE WHEN sale_month = 'January' THEN amount ELSE 0 END) AS January_Sales,
    SUM(CASE WHEN sale_month = 'February' THEN amount ELSE 0 END) AS February_Sales
FROM sales
GROUP BY product;

3️⃣ Click Run and analyze the results.

πŸ“Œ Code Execution Flow

1️⃣ Creating the Sales Table

  • The sales table is created with:

    • sale_id (Primary Key)

    • product (VARCHAR, name of the product)

    • sale_month (VARCHAR, records month of sale)

    • amount (DECIMAL, sales amount)

2️⃣ Inserting Sample Data

  • Four records are inserted:

3️⃣ Pivoting Data with CASE Statements

  • The SELECT query aggregates sales for each product while transforming sale_month values into separate columns.

  • The SUM(CASE WHEN sale_month = 'January' THEN amount ELSE 0 END) AS January_Sales statement ensures that:

    • The sales amount is counted only for January.

    • If not January, it defaults to 0.

  • Similarly, another SUM(CASE WHEN sale_month = 'February' THEN amount ELSE 0 END) AS February_Sales is applied for February.

4️⃣ Final Output Table

  • The pivoted table presents product-wise monthly sales totals:


βœ… Expected Output Breakdown

πŸ”Ή Key Takeaways

βœ” CASE + SUM is used for pivoting rows into columns, making data more readable for analysis.
βœ” Grouping by product ensures monthly totals are calculated separately for each product.
βœ” This method is helpful for sales reports, performance tracking, and analytical dashboards.


πŸ” What’s Happening?
βœ” Transforms month-wise sales data into separate columns for better readability.

πŸ’‘ Best Practices for Pivoting Data:
βœ” Use PIVOT for categorical data transformation.
βœ” Consider aggregating measures (SUM, COUNT) for better insights.
βœ” For databases without PIVOT, use CASE WHEN with GROUP BY as shown above.

UPGRADE TO ANNUAL/ELITE ACCELERATOR


🐍 Python Challenge - Decorators (Deep Dive & Use Cases)

πŸ‘‰ Question: What will be the output of this Python code?

πŸ”˜ A) 'Before function' -> 'Hello!' -> 'After function'
πŸ”˜ B) 'Hello!'
πŸ”˜ C) Error
πŸ”˜ D) 'After function' -> 'Hello!' -> 'Before function'

βœ… Answer: A) 'Before function' -> 'Hello!' -> 'After function'

πŸ“– Explanation:
Python decorators allow us to modify function behavior by wrapping them. This is useful for logging, authentication, and function execution timing.

πŸ”Ή Where It’s Used in Real-World Applications?
βœ… API Rate Limiting: Restricting API calls using decorators.
βœ… Logging Function Calls: Automatically log function execution time.
βœ… Access Control: Enforcing authentication in web applications.


Run & Test on onecompiler.com

1️⃣ Open onecompiler.com, select Python.
2️⃣ Copy and paste the following script:

def decorator_function(original_function):
    def wrapper_function():
        print("Before function execution")
        original_function()
        print("After function execution")
    return wrapper_function

@decorator_function
def say_hello():
    print("Hello!")

say_hello()

3️⃣ Click Run and observe the output.

πŸ“Œ Code Execution Flow

1️⃣ Defining the Decorator Function:

  • decorator_function(original_function) takes another function as an argument.

  • Inside, wrapper_function() is defined to execute additional code before & after calling original_function().

  • The decorator returns wrapper_function, effectively wrapping the original function.

2️⃣ Applying the Decorator (@decorator_function)

  • The say_hello() function is wrapped by decorator_function, meaning that when say_hello() is called, the wrapper executes instead.

3️⃣ Calling say_hello()

  • The decorator ensures that:

    • "Before function execution" is printed.

    • say_hello() runs and prints "Hello!".

    • "After function execution" is printed.


πŸ” What’s Happening?
βœ” Function execution is wrapped with additional logic before and after.

πŸ’‘ Best Practices for Using Decorators:
βœ” Use @functools.wraps to preserve function metadata.
βœ” Stack multiple decorators for added functionality.
βœ” Avoid modifying function parameters inside decorators unless necessary.

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