Skip to main content
SQL • LESSON 105

CTE Filtering

How can we calculate department averages and then filter them?

Advanced3 Minutes1180 XP
🤔 THE QUESTION

How can we calculate department averages and then filter them?

💡 WHAT IS IT?

Filtering a CTE in the final SELECT query simplifies post-aggregation analysis without complex HAVING clauses.

🎯 WHAT IS IT USED FOR?

High-paying department discovery, anomaly detection, threshold filtering on staged metrics.

💻 EXAMPLE
WITH department_salary AS (
    SELECT department_id, AVG(salary) AS average_salary
    FROM employees
    GROUP BY department_id
)
SELECT *
FROM department_salary
WHERE average_salary > 90000;

🎯 Mission Objectives

Practice typing production-grade SQL code for CTE Filtering.

  • CTE filtering
  • WHERE on CTE output
  • Simplified thresholding
  • Clean analytical queries