Skip to main content
SQL โ€ข LESSON 102

CTE with Aggregation

How can we calculate department employee counts using a CTE?

Advancedโฑ 3 Minutesโญ 1150 XP
๐Ÿค” THE QUESTION

How can we calculate department employee counts using a CTE?

๐Ÿ’ก WHAT IS IT?

CTEs encapsulate aggregations so downstream queries can treat summary statistics as regular tabular data.

๐ŸŽฏ WHAT IS IT USED FOR?

Intermediate summary tables, metric staging, pre-calculated department statistics.

๐Ÿ’ป EXAMPLE
WITH department_counts AS (
    SELECT department_id, COUNT(*) AS employee_count
    FROM employees
    GROUP BY department_id
)
SELECT *
FROM department_counts;

๐ŸŽฏ Mission Objectives

Practice typing production-grade SQL code for CTE with Aggregation.

  • โœ“ CTE with GROUP BY
  • โœ“ Summary staging
  • โœ“ Encapsulated aggregation
  • โœ“ Modular SQL