SQL WITH Clause with examples | SQL CTE (Common Table Expression) | CTE vs Subquery in SQL

Published: 07 July 2025
on channel: Crack DE with Mradul
231
4

🚀 In this video, we compare CTEs (Common Table Expressions) with Subqueries using a real-world SQL interview question:

"Get employees who earn more than the company average salary, and also show their department's average salary."

We cover:
✅ How to write the query using multiple subqueries
✅ How to rewrite the same logic using CTEs for better readability
✅ When to use Subqueries vs CTEs
✅ Real interview insights and optimization tips

📌 Perfect for:

SQL learners

Data analyst & data engineering interviews

Anyone who wants to clean up complex SQL logic

-- 1. Create table

CREATE TABLE employees (
emp_id INT PRIMARY KEY,
name VARCHAR(100),
department VARCHAR(50),
salary INT
);

-- 2. Insert values

INSERT INTO employees (emp_id, name, department, salary) VALUES
(1, 'Alice', 'HR', 50000),
(2, 'Bob', 'IT', 70000),
(3, 'Charlie', 'HR', 60000),
(4, 'David', 'IT', 80000),
(5, 'Eva', 'Sales', 45000),
(6, 'Frank', 'Sales', 55000);

💡 Stay till the end for bonus tips on SQL readability and performance!

👇 Don’t forget to:
✅ Like
✅ Comment
✅ Subscribe
🔔 Hit the bell icon for more awesome content!

#SQL #DataEngineering #CTE #CrackDeWithMradul


On this page of the site you can watch the video online SQL WITH Clause with examples | SQL CTE (Common Table Expression) | CTE vs Subquery in SQL with a duration of hours minute second in good quality, which was uploaded by the user Crack DE with Mradul 07 July 2025, share the link with friends and acquaintances, this video has already been watched 231 times on youtube and it was liked by 4 viewers. Enjoy your viewing!