Advanced SQL Learning Guide (College
Level)
1. 🧭 Advanced Core Concepts
**Common Table Expressions (CTEs) & Recursion**
- Use `WITH ... AS (...)` to simplify complex queries by building steps.
- Recursive CTEs model hierarchies (e.g. org charts, folder structures) elegantly.
Exercise:
Build an employee-manager hierarchy using a self-referencing Employees table.
Explain: How recursive CTE builds level-by-level, avoids self-joins.
Window Functions
- Use `OVER(PARTITION BY … ORDER BY …)` along with `ROW_NUMBER()`, `RANK()`,
`LEAD()`, `LAG()`, `SUM()` for:
- Running totals
- Comparing consecutive rows (deltas)
- Top-N queries per group
Exercises:
1. Use `ROW_NUMBER()` to identify each customer’s 3 most recent orders.
2. Use `LAG()` to calculate the month-over-month change in monthly sales.
Explain: How partitioning and ordering control window context.
2. 🧭 Real-World Case Studies
A. Retail Sales Analysis
- Find top-selling items, sales trends, customer demographics.
Exercise Queries:
-- Top 10 products
SELECT product_name, SUM(quantity) AS total_sold FROM sales GROUP BY product_name
ORDER BY total_sold DESC LIMIT 10;
-- Monthly sales trend
SELECT TO_CHAR(order_date, 'YYYY-MM') AS month, SUM(amount) AS total_sales FROM
orders GROUP BY month ORDER BY month;
Explain: How to group dates and aggregate — then transform into business insights.
B. Danny’s Diner (Restaurant Analytics)
Analyze visits, spending patterns, favorite menu items.
Exercises:
- Compute average spend per visit per customer.
- Rank menu items by total revenue.
- Identify repeat vs one-time visitors.
Explain: Linking tables, using `GROUP BY`, window functions for ranking.
C. Customer Behavior in Retail
Join sales, user, product, and loyalty program tables to track behavior.
Exercises:
- Count purchases before and after a user joins loyalty.
- Analyze total spend by loyalty status.
- Identify the most engaged users.
Explain: Multi-table joins, filtering via `WHERE`, analyzing segmentation impact.
3. 🧭 Optimization & Tuning
Indexes & Execution Plans
- Learn how adding/removing indexes influences performance.
- Use `EXPLAIN` or execution plans to track full-table scans vs indexed searches.
Exercise:
Monitor a slow query, add an index on WHERE-clause column(s), measure performance
gain.
Explain: Index selectivity, covering indexes, and how tuning transforms query behavior.
4. 🧭 Design & Architecture
Database Normalization & Schema Design
- Define PKs, FKs, unique constraints, and normal forms.
Views, Stored Procedures, & Transactions
- Create views for reusable business logic.
- Build stored procs/functions for repeated operations.
- Use transactions with `BEGIN`, `COMMIT`, `ROLLBACK`.
Exercise:
- Create a MonthlySales view.
- Write a proc to move a customer to VIP if they exceed a threshold.
5. 🧭 Portfolio Projects & Prerequisites
Suggested End-to-End Projects
- Kickstarter, Crunchbase, CIA Factbook datasets.
- Projects involving fraud detection, financial reports, or e-commerce analytics.
Project Steps:
1. Choose dataset.
2. Load into DB.
3. Define schema and populate.
4. Write varied queries (CTEs, windows, aggregates).
5. Optimize and document.
Goal: Build a polished portfolio with real-world datasets and analytical insights.
6. ✅Practice Resources
- LearnSQL.com: 10 advanced JOIN, window, recursive CTE exercises.
- 8 Week SQL Challenge / GitHub: real-case solutions like Danny's Diner.
- CodeChef SQL case studies: +80 real‑life problems.
- SQL Murder Mystery: Fun challenge.
🎯 Suggested 15-Minute Action Plan
| Step | Task |
|-----------|---------------------------------------|
| 0–5 min | Pick a case (e.g., Danny’s Diner). |
| 5–10 min | Write 2–3 queries with functions. |
| 10–13 min | Optimize: index, explain plan. |
| 13–15 min | Document purpose & snapshot results. |