Skip to main content
SQL • LESSON 113

SUM with CASE

How can we calculate salary totals for a selected department?

Advanced3 Minutes1260 XP
🤔 THE QUESTION

How can we calculate salary totals for a selected department?

💡 WHAT IS IT?

SUM(CASE WHEN ... THEN value ELSE 0 END) aggregates specific subset amounts into dedicated metric columns.

🎯 WHAT IS IT USED FOR?

Department payroll splits, online vs in-store revenue totals, regional revenue attribution.

💻 EXAMPLE
SELECT
SUM(CASE
    WHEN department = 'Engineering' THEN salary
    ELSE 0
END) AS engineering_salary
FROM employees;

🎯 Mission Objectives

Practice typing production-grade SQL code for SUM with CASE.

  • SUM(CASE ...)
  • Selective summation
  • Payroll attribution
  • Conditional totals