CASE WHEN statements provide great flexibility when dealing with buckets of results or when you need to find a way to filter out certain results. Another way to think of it is it's a conditional logic similar to IF-THEN statements in other programming languages.
When using a CASE WHEN statement, it's important to remember you need a condition, what to do when that condition is met, and an END clause. A simple syntax is below:
CASE WHEN condition THEN result ELSE other_result END
You can include multiple conditional buckets as needed by adding more WHEN and THEN clauses to your statement, as follows:
SELECT
CASE WHEN condition THEN result WHEN condition_2 THEN result_2
WHEN condition_2
THEN result_2 ELSE other_result END AS new_column_name
For example,
SELECT CASE WHEN country = 'USA' THEN 'North America' WHEN country = 'Mexico' THEN 'North America' ELSE country END AS 'West Region'
Operators:
Equals: =
Not Equals: !=
Greater than: >
Less than: <
Greater or equal: >=
Less or equal: <=
Contains: "Like"
Does not contain: "Not Like"