The SQL language supports a couple of different ways of creating segments from data, depending on the dialect. Just like in Google Sheets, we can use an IF function to evaluate an expression in SQL. However, IF statements aren't supported in every SQL dialect (yes in MySQL, SQL Server, no in PostgreSQL), and even when it is supported, it isn't the primary method in SQL to evaluate expressions. That is accomplished by the use of the CASE WHEN statement.
CASE WHEN Syntax
Evaluating expressions in SQL using the CASE WHEN statement is very similar to the IF function logic, and has the following syntax:
CASE WHEN condition to be evaluated
THEN value if true
ELSE value if false
END AS name of segment
Let's use an example to see what that looks like in practice. Imagine that you would like to characterize transactions high (>= $500) or low (< $500) from your list of payments. The CASE WHEN statement would look like this:
WHEN payment_amount >= 500 THEN 'HIGH'
WHEN payment_amount < 500 THEN 'LOW'
The main difference in syntax between the CASE WHEN statement and an IF function is the addition of END, which is used to complete the conditional statement, and AS, which is used to label the result of the CASE WHEN statement and give a name to your segment.