Module 04
Complex Queries & CTEs
Isolate nested expression logic grids and structure enterprise queries cleanly via temporary Common Table Expressions.
📋 Master Dataset Base
| name | department | salary |
|---|---|---|
| Amit | Tech | 80000 |
| Rohan | HR | 55000 |
| Neha | Tech | 90000 |
| Vikram | HR | 60000 |
💡 Company-Wide Average Salary Value Baseline: ₹71,250
1. Nested Subqueries
Definition: An inner lookup script nested deep inside an outer statement block to dynamic-filter results.
SELECT name, salary FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);
| name | salary |
|---|---|
| Amit | 80000 |
| Neha | 90000 |
2. CTEs (Common Table Expressions)
Definition: Declares a neat virtual named table query temporarily using the WITH operator for complex calculations.
WITH DeptKPI AS (
SELECT department, AVG(salary) AS d_avg
FROM employees
GROUP BY department
)
SELECT e.name, e.department, d.d_avg
FROM employees e
JOIN DeptKPI d ON e.department = d.department;| name | department | d_avg |
|---|---|---|
| Amit | Tech | 85000 |
| Neha | Tech | 85000 |
| Rohan | HR | 57500 |
| Vikram | HR | 57500 |
Unlock Real Corporate Scenario Interactive Sandboxes
Basic structures are covered! To clear FAANG, MAANG & top analytical enterprise interviews, buy the complete pack to get 150+ interactive practice question sheets, video explanation keys, and real production datasets.