
Applying Logic with CASE
Introduction
In many programming languages, you use if -> then statements to apply logic.
In programming languages, you do this as follows:
if x > 0:
print("The value is positive")
if x < 0:
print("The value is negative")
else:
print("The value is zero")
To apply if -> then logic in SQL, you use a CASE statement.
You also use WHEN, THEN, ELSE, and END expressions.
The above example would look like this in SQL:
CASE
WHEN x > 0 then "The value is positive"
WHEN x < 0 then "The value is negative"
ELSE "The value is zero"
END
1. CASE example
Let's look at the following example:
SELECT
name,
year,
CASE
WHEN year > 1986 THEN 'After 1986'
WHEN year = 1986 THEN 'From 1986'
ELSE 'Before 1986'
END
FROM
movies;
| name | year | case |
|---|---|---|
| Aliens | 1986 | From 1986 |
| Animal House | 1978 | Before 1986 |
| Apollo 13 | 1995 | After 1986 |
| ... | ... | ... |
Here's what happens:
- We select columns
nameandyearfrom tablemovies. - We apply a
CASEstatement.- If (
WHEN) values in columnyearare greater than1986, then (THEN) we get valueAfter 1986. - If (
WHEN) values in columnyearare equal to1986, then (THEN) we get valueFrom 1986. - Otherwise (
ELSE) we get the valueBefore 1986.
- If (
- With
END, we end theCASE statement.
The values from the CASE statement appear in a new column: case.
With this, you have added a new column using logic.
You can use comparison operators like =, >, and >= for comparisons.
2. Naming a new column from CASE with AS
We have seen that the new column is named case by default.
This is not always desired.
By using AS, you can give the column a specific name.
We do this in the following example:
SELECT
name,
year,
CASE
WHEN year > 1986 THEN 'After 1986'
WHEN year = 1986 THEN 'From 1986'
ELSE 'Before 1986'
END AS year_category
FROM
movies;
| name | year | year_category |
|---|---|---|
| Aliens | 1986 | From 1986 |
| Animal House | 1978 | Before 1986 |
| Apollo 13 | 1995 | After 1986 |
| ... | ... | ... |
We added after END: AS year_category.
You see that the new column is now called year_category instead of case.
3. Using functions in combination with CASE
With a CASE statement, you compare scenarios.
You can use comparison operators like =, >, and >=.
You can also use functions.
We do this in the following example:
SELECT
name,
CASE
WHEN LENGTH(name) >= 10 THEN 'Long name'
ELSE 'Short length'
END AS name_length_category
FROM
movies;
| name | name_length_category |
|---|---|
| Aliens | Short length |
| Animal House | Long name |
| Apollo 13 | Short length |
| ... | ... |
- With the function
LENGTH(), we determine the length of a movie's name. - Depending on the length, we create the new column
name_length_categorywith theCASEstatement.
4. Using logical operators in combination with CASE
With a CASE statement, you compare scenarios.
You can use comparison operators like =, >, and >=.
You can also use logical operators like AND, OR, and IN.
We do this in the following example:
SELECT
name,
CASE
WHEN LENGTH(name) >= 10 THEN 'Long name'
WHEN (
LENGTH(name) > 5
AND LENGTH(name) < 10
) THEN 'Medium length'
ELSE 'Short length'
END AS name_length_category
FROM
movies;
| name | name_length_category |
|---|---|
| Aliens | Medium length |
| Animal House | Long name |
| Apollo 13 | Medium length |
| ... | ... |
- With the function
LENGTH(), we determine the length of a movie's name. - Depending on the length, we create the new column
name_length_categorywith theCASEstatement. - With the logical operator
AND, we create the categoryMedium length, where the length is between5and10characters.
Summary
- With a
CASEstatement, you can apply logic.- You also use
WHEN,THEN,ELSE, andEND. - With this, you can add a column using comparisons.
- For comparisons, you can use comparison operators like
=,>, and>=. - Example of a
CASEstatement:
- You also use
SELECT
name,
year,
CASE
WHEN year > 1986 THEN 'After 1986'
WHEN year = 1986 THEN 'From 1986'
ELSE 'Before 1986'
END
FROM
movies;
- With
AS, you can give the new column a specific name, for example:
SELECT
name,
year,
CASE
WHEN year > 1986 THEN 'After 1986'
WHEN year = 1986 THEN 'From 1986'
ELSE 'Before 1986'
END AS year_category
FROM
movies;
- You can use functions in comparisons.
- You can use logical operators like
AND,OR, andIN.