SQL CASE in SELECT

The most common place CASE appears — creating an entirely new, derived column that doesn't exist anywhere in the actual table.

What & Why

Every example so far in this series has already been doing this — but it's worth calling out explicitly: CASE inside SELECT is how a query manufactures a brand-new column from existing data. The table itself never had a "spend_flag" column; the query creates it on the fly, purely from the logic inside the CASE expression.

See How It Works

BUSINESS QUESTION

Marketing wants campaign status converted into an analyst-friendly delivery label.

idnamechannelspendstart_dateend_datestatustarget_segment
1Spring Launchgoogle_ads55000.002024-01-152024-03-31activesmb
2Retention Webinaremail45000.002024-02-102024-04-15activeenterprise
3Finance Retargetinglinkedin50000.002024-03-122024-05-31activeenterprise
4Enterprise Searchgoogle_ads60000.002024-04-012024-06-30activeenterprise
EXAMPLE QUERY
SELECT
  name,
  status,
  CASE WHEN status = 'active' THEN 'in market' ELSE 'not running' END AS delivery_state
FROM marketing.campaigns
ORDER BY name;
RESULT — exact output from the displayed Queryflo rows
namestatusdelivery_state
Enterprise Searchactivein market
Finance Retargetingactivein market
Retention Webinaractivein market
Spring Launchactivein market

CASE adds a derived delivery_state without changing marketing.campaigns.

Now You Try

Practice this concept

Marketing wants campaign status converted into an analyst-friendly delivery label.

Available schema
marketing

Prefix tables with marketing.table_name.

namestatusdelivery_state
marketing.campaigns
ColumnType
idinteger
nametext
channeltext
spendnumeric
start_datedate
end_datedate
statustext
target_segmenttext
legacy_idtext
query.sql
Intermediate business practice

Sign up free to try it on a real business scenario