SQL CASE ELSE

Supplies a fallback value for rows that don't match any WHEN condition — without it, non-matching rows silently become NULL.

What & Why

ELSE value goes right before END, and it's what a row falls back to when none of the WHEN conditions above it matched. Without an explicit ELSE, that fallback is always NULL — which the CASE WHEN lesson touched on briefly. This lesson is about replacing that default with something more useful.

See How It Works

BUSINESS QUESTION

Product wants every feedback rating classified, including ratings outside the positive rule.

iduser_idfeature_idratingnps_scorecommentcreated_at
1101159Clear and useful2024-01-25 12:15:00+00
210224NULLExport worked well2024-02-20 15:40:00+00
3103147Helpful setup flow2024-03-18 09:05:00+00
410433NULLNeeds clearer timing2024-04-08 17:30:00+00
EXAMPLE QUERY
SELECT
  id AS feedback_id,
  rating,
  CASE WHEN rating >= 4 THEN 'positive' ELSE 'not positive' END AS rating_class
FROM product.feedback
ORDER BY feedback_id;
RESULT — exact output from the displayed Queryflo rows
feedback_idratingrating_class
15positive
24positive
34positive
43not positive

ELSE gives the final feedback row an explicit fallback label.

Now You Try

Practice this concept

Product wants every feedback rating classified, including ratings outside the positive rule.

Available schema
product

Prefix tables with product.table_name.

feedback_idratingrating_class
product.feedback
ColumnType
idinteger
user_idinteger
feature_idinteger
ratinginteger
nps_scoreinteger
commenttext
created_attimestamp with time zone
moderation_buckettext
query.sql
Intermediate business practice

Sign up free to try it on a real business scenario