Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Simple CASE WHEN
Searched CASE WHEN
CASE WHEN with aggregates
CASE WHEN with GROUP BY
Summary
24. Summary

Instruction

Okay then, it's time to revise the most important aspects of CASE WHEN.

  • A simple CASE WHEN has the following syntax:
    CASE column_name
      WHEN value1 THEN text1
      WHEN value2 THEN text2
      ...
      ELSE text_else
    END
    
  • A searched CASE WHEN has the following syntax:
    CASE
      WHEN condition1 THEN text1
      WHEN condition2 THEN text2
      ...
      ELSE text_else
    END
    
  • The ELSE part is optional.
  • Remember to put the END clause at the end.
  • You can use CASE WHEN with SUM to count multiple values in a single query:
    SUM(CASE WHEN x THEN 1 ELSE 0 END)
  • Similarly, you can use CASE WHEN with COUNT to count multiple values in a single query:
    COUNT(CASE WHEN x THEN column END)

Exercise

Are you ready for some practice? Let's find out!