Skip to Content
Navigation:

A stick figure smiling

Here's a preview from my zine, Become a SELECT Star!! If you want to see more comics like this, sign up for my saturday comics newsletter or browse more comics!

Image of a comic. To read the full HTML alt text, click "read the transcript". get the zine!
read the transcript!

Often I want to categorize by something that isn’t a column:

person 1: I want to count children/adults/teenagers but there’s no column for that!

person 2: no problem! jut categorize people based on age!

CASE is how to write an if statement in SQL. Here’s the syntax:

CASE
  WHEN <condition>        THEN <result>
  WHEN <other-condition>  THEN <result>
  ...
  ELSE <result>
END

example:

Here’s how to categorize people into age ranges!

SELECT first_name, age, CASE
  WHEN age < 13  THEN 'child'
  WHEN age < 20  THEN 'teenager'
  ELSE 'adult' END AS age_range
FROM people

(returns first THEN where the condition matches)

people:

first_name age
ahmed 5
marle 17
akira 60
pablo 15

result:

first_name age age_range
ahmed 5 child
marle 17 teenager
akira 60 adult
pablo 15 teenager