runtime
14 / 88

lesson 14 Calculations & data quality · intermediate

Make decisions with CASE

Choose a result for each row using conditional logic.

the idea

CASE evaluates its WHEN conditions in order and returns the first matching result; ELSE covers everything else. Put the case for missing values first so an unknown temperature is not silently labelled by ELSE.

Dataset: Weather

The tables the challenges use, with their first 6 rows. Under each column name: the kind of value it holds, and which table it links to.

daily_weather10 columns · 109,214 rows
station_idtext · links to stationsobservation_datedate · links to calendartmax_cdecimaltmin_cdecimalprecipitation_mmdecimalprecip_tracenumbersnowfall_mmdecimalsnow_depth_mmdecimalavg_wind_speed_msdecimalpeak_gust_msdecimal
USW000031312022-01-0117.27.200NULLNULL26.3
USW000031312022-01-0220500NULLNULL1.15.4
USW000031312022-01-0317.83.900NULLNULL1.14.5
USW000031312022-01-0417.24.400NULLNULL1.44.5
USW000031312022-01-0521.15.600NULLNULL0.94
USW000031312022-01-0622.27.200NULLNULL0.63.6
station_idtext · links to stations
USW00003131
observation_datedate · links to calendar
2022-01-01
tmax_cdecimal
17.2
tmin_cdecimal
7.2
precipitation_mmdecimal
0
precip_tracenumber
0
snowfall_mmdecimal
NULL
snow_depth_mmdecimal
NULL
avg_wind_speed_msdecimal
2
peak_gust_msdecimal
6.3
station_idtext · links to stations
USW00003131
observation_datedate · links to calendar
2022-01-02
tmax_cdecimal
20
tmin_cdecimal
5
precipitation_mmdecimal
0
precip_tracenumber
0
snowfall_mmdecimal
NULL
snow_depth_mmdecimal
NULL
avg_wind_speed_msdecimal
1.1
peak_gust_msdecimal
5.4
station_idtext · links to stations
USW00003131
observation_datedate · links to calendar
2022-01-03
tmax_cdecimal
17.8
tmin_cdecimal
3.9
precipitation_mmdecimal
0
precip_tracenumber
0
snowfall_mmdecimal
NULL
snow_depth_mmdecimal
NULL
avg_wind_speed_msdecimal
1.1
peak_gust_msdecimal
4.5
station_idtext · links to stations
USW00003131
observation_datedate · links to calendar
2022-01-04
tmax_cdecimal
17.2
tmin_cdecimal
4.4
precipitation_mmdecimal
0
precip_tracenumber
0
snowfall_mmdecimal
NULL
snow_depth_mmdecimal
NULL
avg_wind_speed_msdecimal
1.4
peak_gust_msdecimal
4.5
station_idtext · links to stations
USW00003131
observation_datedate · links to calendar
2022-01-05
tmax_cdecimal
21.1
tmin_cdecimal
5.6
precipitation_mmdecimal
0
precip_tracenumber
0
snowfall_mmdecimal
NULL
snow_depth_mmdecimal
NULL
avg_wind_speed_msdecimal
0.9
peak_gust_msdecimal
4
station_idtext · links to stations
USW00003131
observation_datedate · links to calendar
2022-01-06
tmax_cdecimal
22.2
tmin_cdecimal
7.2
precipitation_mmdecimal
0
precip_tracenumber
0
snowfall_mmdecimal
NULL
snow_depth_mmdecimal
NULL
avg_wind_speed_msdecimal
0.6
peak_gust_msdecimal
3.6

first 6 of 109,214 rows · your queries use all of them

worked example

Run these steps in order to see the idea before you write your own query. The step marked “the pattern” is what the challenges ask for. Running the example does not count toward progress.

  1. Label days as wet or dry. Run this. CASE checks each row and returns wet when precipitation_mm is above zero, otherwise dry.

    step-1.sql
    SELECT
      station_id,
      observation_date,
      CASE
        WHEN precipitation_mm > 0 THEN 'wet'
        ELSE 'dry'
      END AS day_type
    FROM daily_weather
    WHERE station_id = 'USW00094728'
    LIMIT 15;
  2. Handle NULL before other labels. Run this. Put IS NULL first in CASE so missing snowfall is not mistaken for zero.

    step-2.sql
    SELECT
      observation_date,
      snowfall_mm,
      CASE
        WHEN snowfall_mm IS NULL THEN 'not reported'
        WHEN snowfall_mm > 0 THEN 'snow'
        ELSE 'none'
      END AS snow
    FROM daily_weather
    WHERE station_id = 'USW00093197'
    LIMIT 15;
  3. Put it together. The lesson's pattern in one query.the pattern

    example.sql
    SELECT
      station_id,
      observation_date,
      CASE
        WHEN precipitation_mm > 0 THEN 'wet'
        ELSE 'dry'
      END AS day_type
    FROM daily_weather
    WHERE station_id = 'USW00094728';

common mistake

CASE WHEN tmax_c < 0 THEN 'freezing' ELSE 'above freezing' END labels a missing temperature as above freezing.

challenge 1

For station USW00094728 (Central Park), return observation_date, tmax_c and band: 'unknown' when tmax_c is NULL, 'freezing' below 0, 'hot' at 30 or above, otherwise 'mild'.

what to return

Columns
observation_datetmax_cband in this order
Rows
any order
challenge-1.sql
 
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.
Make decisions with CASE — Calculations & data quality · Runtime