Introduction
Simple CASE WHEN
Searched CASE WHEN
CASE WHEN with aggregates
CASE WHEN with GROUP BY
Revision
24. Summary

Instruction

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

  • A simple CASE 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 about 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!

Console

Code editor

Result

TableConsole