Databases · Junior · Question 60 of 75

What does CASE do in a query?

SELECT name,
  CASE WHEN age < 18 THEN 'minor' ELSE 'adult' END AS group_name
FROM users;

Explanation

It's often used inside aggregates too: COUNT(*) FILTER (WHERE ...) or SUM(CASE WHEN ... THEN 1 ELSE 0 END) to count by category.

Further reading

Problem with a question

Databases · Junior · question 60

Test yourself

Take the Databases · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz