Skip to content

CASE

SELECT
name,
CASE
WHEN age < 18 THEN 'Child'
WHEN age BETWEEN 18 AND 64 THEN 'Adult'
ELSE 'Senior'
END AS age_group
FROM
people;
| name | age_group |
|-------|-----------|
| Maria | Adult |
| John | Senior |
| Alice | Child |
| Bob | Adult |

In the SQL SELECT statement, the CASE statement evaluates the age column. If the age is less than 18, it returns ‘Child’. If the age is between 18 and 64, it returns ‘Adult’. Else, it returns ‘Senior’. These results are labeled as ‘age_group’ in the final output.