0% found this document useful (0 votes)
16 views3 pages

Why Use A CTE?: Common Table Expression (CTE)

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

Why Use A CTE?: Common Table Expression (CTE)

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

11/24/24, 10:24 PM CTE

CTE
A Common Table Expression (CTE) in SQL is like a temporary, reusable table that
you create within a query. You can use it to make your query easier to read and
manage, especially when dealing with complex queries or subqueries.
CTEs are declared using the WITH keyword and exist only for the duration of the
query they are part of.

Why Use a CTE?


• Improves readability: Breaks complex queries into smaller, manageable parts.

• Reusable: The same CTE can be referenced multiple times in the main query.

• Clean code: Avoids repeating subqueries.

Syntax of a CTE

WITH cte_name AS ( -- Your CTE query here SELECT column1, column2 FROM ta
ble_name WHERE condition ) -- Main query that uses the CTE SELECT * FROM
cte_name;

Step 1: Create Table

CREATE TABLE employees ( EmployeeID INT PRIMARY KEY, Name VARCHAR(50), De


partment VARCHAR(50), Salary INT, JoiningDate DATE );

Step 2: Insert Data

https://sponge-tick-989.notion.site/CTE-144619fbb33d80d8a5d5cbb10c5605c0 1/3
11/24/24, 10:24 PM CTE

INSERT INTO employees (EmployeeID, Name, Department, Salary, JoiningDate)


VALUES (1, 'Alice', 'HR', 60000, '2021-01-15'), (2, 'Bob', 'IT', 80000,
'2020-06-10'), (3, 'Charlie', 'HR', 50000, '2022-03-20'), (4, 'David', 'I
T', 70000, '2019-09-05'), (5, 'Eva', 'Finance', 75000, '2021-07-25'), (6,
'Frank', 'Finance', 55000, '2020-11-11');

Step 3: Practical Implementation with Questions

Question 1:
Find the average salary of each department using a CTE.

WITH AvgSalary AS ( SELECT Department, AVG(Salary) AS AvgSalary FROM


employees GROUP BY Department ) SELECT * FROM AvgSalary;

Question 2:
List employees earning above their department’s average salary.

WITH AvgSalary AS ( SELECT Department, AVG(Salary) AS AvgSalary FROM


employees GROUP BY Department ) SELECT e.Name, e.Department, e.Salary
FROM employees e JOIN AvgSalary a ON e.Department = a.Department WHERE
e.Salary > a.AvgSalary;

Question 3:
Find the most recently joined employee in each department.

WITH RecentJoiners AS ( SELECT Department, MAX(JoiningDate) AS


RecentJoiningDate FROM employees GROUP BY Department ) SELECT e.Name,
e.Department, e.JoiningDate FROM employees e JOIN RecentJoiners r ON
e.Department = r.Department AND e.JoiningDate = r.RecentJoiningDate;

https://sponge-tick-989.notion.site/CTE-144619fbb33d80d8a5d5cbb10c5605c0 2/3
11/24/24, 10:24 PM CTE

Question 4:
Find the total salary paid in departments where the average salary is above 60,000

WITH AvgSalary AS ( SELECT Department, AVG(Salary) AS AvgSalary FROM


employees GROUP BY Department ) SELECT e.Department, SUM(e.Salary) AS
TotalSalary FROM employees e JOIN AvgSalary a ON e.Department =
a.Department WHERE a.AvgSalary > 60000 GROUP BY e.Department;

https://sponge-tick-989.notion.site/CTE-144619fbb33d80d8a5d5cbb10c5605c0 3/3

You might also like