0% found this document useful (0 votes)
5 views2 pages

CTE SQL Examples

The document provides various examples of Common Table Expressions (CTEs) in SQL, showcasing different scenarios such as finding employees with above-average salaries, creating a recursive employee hierarchy, calculating total sales per customer, joining orders with customers, and ranking top employees by salary within departments. Each example includes the SQL query structure and the intended outcome. These examples serve as practical applications of CTEs for data analysis and reporting.
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)
5 views2 pages

CTE SQL Examples

The document provides various examples of Common Table Expressions (CTEs) in SQL, showcasing different scenarios such as finding employees with above-average salaries, creating a recursive employee hierarchy, calculating total sales per customer, joining orders with customers, and ranking top employees by salary within departments. Each example includes the SQL query structure and the intended outcome. These examples serve as practical applications of CTEs for data analysis and reporting.
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/ 2

CTE SQL Query Examples with Different Scenarios

1. Basic CTE - Employees Above Average Salary

Find employees whose salary is above the average salary.

WITH AvgSalary AS (
SELECT AVG(salary) AS avg_salary FROM Employees
)
SELECT emp_id, name, salary
FROM Employees, AvgSalary
WHERE Employees.salary > AvgSalary.avg_salary;

2. Recursive CTE - Employee Hierarchy

Find all employees under a specific manager recursively.

WITH RECURSIVE EmpHierarchy AS (


SELECT emp_id, name, manager_id, 1 AS level
FROM Managers
WHERE emp_id = 1

UNION ALL

SELECT m.emp_id, m.name, m.manager_id, eh.level + 1


FROM Managers m
INNER JOIN EmpHierarchy eh ON m.manager_id = eh.emp_id
)
SELECT * FROM EmpHierarchy;

3. CTE with Aggregation - Customer Sales

Calculate total sales per customer and filter those above 250.

WITH CustomerSales AS (
SELECT customer_id, SUM(amount) AS total_sales
FROM Sales
GROUP BY customer_id
)
SELECT *
FROM CustomerSales
WHERE total_sales > 250;

4. CTE with Join - Orders and Customers

Join customers and their total order amount using a CTE.


WITH OrderTotals AS (
SELECT customer_id, SUM(order_amount) AS total
FROM Orders
GROUP BY customer_id
)
SELECT c.customer_name, o.total
FROM Customers c
JOIN OrderTotals o ON c.customer_id = o.customer_id;

5. CTE for Row Number - Top N per Category

Find top 2 highest paid employees in each department.

WITH RankedSalaries AS (
SELECT emp_id, name, department, salary,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rn
FROM Employees
)
SELECT emp_id, name, department, salary
FROM RankedSalaries
WHERE rn <= 2;

You might also like