0% found this document useful (0 votes)
8 views4 pages

SQL Mastery: Aggregation to Optimization

sql sylabus

Uploaded by

itsaditya9534
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

SQL Mastery: Aggregation to Optimization

sql sylabus

Uploaded by

itsaditya9534
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

🟨 DAY 1 – Aggregation, GROUP BY, HAVING & JOIN Mastery

🎯 Goal

Understand how to summarize, compare, and relate tables for analytical insights.

🧩 Part 1 – Aggregation & GROUP BY

1. Count total orders placed.

2. Find total revenue per payment method.

3. Show average order value per customer.

4. Display total number of orders per customer (use COUNT()).

5. Find monthly sales totals using MONTH(order_date).

6. Show highest, lowest, and average product price per category.

7. Count how many products exist in each category.

🧩 Part 2 – HAVING Clause

8. Show payment methods that generated > ₹5000 in revenue.

9. Display categories where average price > ₹1000.

10. Show customers whose total spending > ₹4000.

🧩 Part 3 – JOIN Practice

11. List each customer with all their order IDs.

12. Show customer_name, order_date, and total_amount.

13. Combine orders, order_details, and products → show each item bought with its price and
quantity.

14. Find total amount spent per city (JOIN + GROUP BY).

15. List customers without any orders (use LEFT JOIN + IS NULL).

🧩 Challenge

Show for each payment method:


total revenue, average order value, and number of unique customers — ordered by revenue DESC.

✅ End of Day 1 Outcome:


You’ll think in “sets” — grouping, joining, and summarizing data like an analyst.
🟦 DAY 2– Subqueries, Constraints, CASE, Views, Transactions

🎯 Goal

Write professional, structured queries with conditions and reusable logic.

🧩 Part 1 – Subqueries

1. List customers who spent above average total_amount.

2. Find products priced above the average product price.

3. Show top 3 orders by amount (use LIMIT inside a subquery).

4. Display all customers who purchased Smart Watch (nested subquery).

🧩 Part 2 – CASE Statements

5. Label each product by price range:

o <1000 → “Budget”

o 1000–3000 → “Mid-Range”

o >3000 → “Premium”

6. Classify customers by total spend:

o >5000 → Gold

o 2000–5000 → Silver

o <2000 → Bronze

🧩 Part 3 – Constraints Practice

7. Add a CHECK constraint: total_amount > 0.

8. Add UNIQUE constraint to [Link].

9. Add FOREIGN KEY from orders.customer_id → customers.customer_id.

🧩 Part 4 – Views

10. Create a view top_customers (spending > ₹5000).

11. Query from that view.

12. Drop or alter the view.


🧩 Part 5 – Transactions

13. Begin a transaction that:

 Inserts a new order

 Reduces product stock

 Commits changes

14. Try the same but ROLLBACK midway to restore data.

✅ End of Day 2 Outcome:


You’ll understand logic, integrity, and modularity — your SQL becomes maintainable and safe.

🟪 DAY 4 – Advanced Analytics, Window Functions & Optimization

🎯 Goal

Use advanced features to analyze data like a professional data engineer.

🧩 Part 1 – Indexes & Optimization

1. Create index on customers(city).

2. Create composite index on (customer_id, order_date).

3. Check query performance before vs. after index (use EXPLAIN if supported).

4. Drop an index.

🧩 Part 2 – Window Functions

5. Rank products by price (RANK() + OVER (ORDER BY price DESC)).

6. Calculate running total of total_amount by order date.

7. Find top 3 customers using DENSE_RANK().

8. Show difference between each order and average order value (total_amount – AVG()
OVER()).

🧩 Part 3 – CTE (Common Table Expressions)

9. Create a CTE showing monthly sales.

10. From that CTE, return only months where sales > ₹4000.

11. Nest a CTE inside another (bonus!).


🧩 Part 4 – String & Date Functions

12. Extract first name from full_name (SUBSTRING / LEFT).

13. Show customers who joined in 2024 (YEAR(join_date)=2024).

14. Concatenate city + email → contact_info.

15. Format dates as Month-Year (DATE_FORMAT).

🧩 Part 5 – Final Project Query 🏁

For each customer, show:

 Full name

 Number of orders

 Total spent

 Average order value

 Membership Tier (CASE WHEN)

 Rank by spending (RANK() OVER())

 Sorted by total spent DESC

(This single query uses JOIN + GROUP BY + CASE + WINDOW FUNCTIONS → complete mastery!)

✅ End of Day 4 Outcome:


You’ll be able to perform reporting, ranking, KPI generation, and optimization — full-stack SQL skills
🎯

🧭 How to Practice Efficiently

 🕒 1.5–2 hours per day

 ⚙️Use your retail_practice_db for all questions

 ✍️Save queries in a .sql file daily ([Link], [Link], [Link])

 📈 Re-run & tweak your queries with different filters

Common questions

Powered by AI

Window functions are preferred over traditional aggregate functions in scenarios where calculations need to be performed across a set of rows related to the current row without collapsing them into a single output. For example, they allow ranking products by price using RANK() over a specified order , or computing a running total for orders by date to understand spending patterns over time (). These functions offer powerful analytical capabilities for cumulative, ranking, or windowed aggregations that are not possible with traditional GROUP BY techniques, as they preserve row output.

Creating an index improves query performance primarily in scenarios where specific columns are frequently used in WHERE clauses, joins, or as sort criteria. For example, creating an index on customers(city) or a composite index on (customer_id, order_date) can significantly enhance performance when querying by these attributes . The impact of indexing can be evaluated using the EXPLAIN statement, which shows how SQL queries are executed and where indexes are used .

The CASE statement allows conditional logic in SQL queries to classify or label data. It can transform outputs based on complex logic without requiring multiple queries. Use-case scenarios include labeling products by price range into categories such as 'Budget', 'Mid-Range', or 'Premium' , or classifying customers into tiers like 'Gold', 'Silver', and 'Bronze' based on their total spend . This functionality enables dynamic segmentation directly within query results, enhancing reporting and analysis.

SQL transactions should be used to group multiple operations into a single unit, ensuring either all or none are executed to maintain data integrity. Key best practices include using BEGIN to start a transaction, COMMIT to save changes, and ensuring atomicity by rolling back incomplete transactions. ROLLBACK can be integrated effectively to restore data during error conditions or business logic failures, such as when inserting a new order, reducing product stock, and needing to revert these changes if an error occurs midway . This ensures that the database remains in a consistent state.

CTEs simplify complex SQL queries by breaking them into reusable named parts, enhancing readability and organization. They are particularly advantageous when performing recursive queries or when intermediate data needs to be reused. Nesting CTEs, like creating a CTE to show monthly sales and nesting it to filter for months where sales exceed ₹4000 , can help craft intricate queries that require multiple layers of abstraction and data manipulation.

SQL JOIN operations allow combining rows from two or more tables based on a related column, enabling comprehensive data analysis. Practical examples include listing each customer with all their order IDs , showing a customer's name, order date, and total amount by joining on relevant customer and orders tables , or finding the total amount spent per city using JOIN and GROUP BY for summary analysis . These operations are essential for analyzing relationships and aggregating data across different datasets.

The HAVING clause is used to filter results in SQL based on aggregate functions. Unlike WHERE, which filters rows before they are grouped, HAVING filters rows after aggregation. It is beneficial in scenarios such as finding payment methods that generated more than ₹5000 in revenue , or displaying categories where the average price exceeds ₹1000 . These conditions use aggregate values and are only evaluated after aggregation has been performed.

Subqueries allow complex queries to be broken into more manageable components, where an inner query's result can be used by an outer query. They can be leveraged to solve problems such as finding products priced above the average, which requires calculating the average product price in a subquery and using it in the main query . Subqueries are essential for filtering, calculating aggregated statistics, or executing multiple-step logical processes that depend on intermediate results.

Structuring SQL queries for maintainability is crucial to ensure that databases remain efficient and up-to-date as needs evolve. Using views allows encapsulation of complex queries in reusable, logical structures, like creating views for top customers with significant spending . Constraints, such as CHECK or UNIQUE, enforce data integrity by preventing invalid or duplicate data, thus simplifying future query writing and maintenance . They contribute to maintainability by promoting consistency, reducing redundancy, and ensuring logical consistency across applications and changes.

Foreign key constraints enforce referential integrity by ensuring the value in one table matches an existing value in another. Challenges include potentially complex cascading updates or deletes, which can lead to performance issues if not carefully managed. They require meticulous design to avoid orphaned records or redundant data. For instance, adding a foreign key from orders.customer_id to customers.customer_id demands that changes in the customers table might require strategic management of dependent orders . These constraints influence database design by necessitating thoughtful planning of table relationships and data dependencies.

You might also like