Skip to main content
SQL โ€ข LESSON 113

SUM with CASE

How can we calculate salary totals for a selected department?

Advancedโฑ 3 Minutesโญ 1260 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