Module 04

Complex Queries & CTEs

Isolate nested expression logic grids and structure enterprise queries cleanly via temporary Common Table Expressions.

📋 Master Dataset Base

namedepartmentsalary
AmitTech80000
RohanHR55000
NehaTech90000
VikramHR60000

💡 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);
namesalary
Amit80000
Neha90000

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;
namedepartmentd_avg
AmitTech85000
NehaTech85000
RohanHR57500
VikramHR57500

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.