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

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
USW000031312022-01-0716.17.200NULLNULL1.84.5
USW000031312022-01-0817.28.300NULLNULL1.54.5
USW000031312022-01-09256.700NULLNULL14.5
USW000031312022-01-1023.37.800NULLNULL1.44
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
station_idtext · links to stations
USW00003131
observation_datedate · links to calendar
2022-01-07
tmax_cdecimal
16.1
tmin_cdecimal
7.2
precipitation_mmdecimal
0
precip_tracenumber
0
snowfall_mmdecimal
NULL
snow_depth_mmdecimal
NULL
avg_wind_speed_msdecimal
1.8
peak_gust_msdecimal
4.5
station_idtext · links to stations
USW00003131
observation_datedate · links to calendar
2022-01-08
tmax_cdecimal
17.2
tmin_cdecimal
8.3
precipitation_mmdecimal
0
precip_tracenumber
0
snowfall_mmdecimal
NULL
snow_depth_mmdecimal
NULL
avg_wind_speed_msdecimal
1.5
peak_gust_msdecimal
4.5
station_idtext · links to stations
USW00003131
observation_datedate · links to calendar
2022-01-09
tmax_cdecimal
25
tmin_cdecimal
6.7
precipitation_mmdecimal
0
precip_tracenumber
0
snowfall_mmdecimal
NULL
snow_depth_mmdecimal
NULL
avg_wind_speed_msdecimal
1
peak_gust_msdecimal
4.5
station_idtext · links to stations
USW00003131
observation_datedate · links to calendar
2022-01-10
tmax_cdecimal
23.3
tmin_cdecimal
7.8
precipitation_mmdecimal
0
precip_tracenumber
0
snowfall_mmdecimal
NULL
snow_depth_mmdecimal
NULL
avg_wind_speed_msdecimal
1.4
peak_gust_msdecimal
4

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

worked example

The step marked “the pattern” is what the challenges build on. Running examples doesn’t 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

Find every day at the Central Park station (station_id 'USW00094728').

Return observation_date, tmax_c, and band.

band is unknown when tmax_c is missing, freezing when it is below 0, hot when it is 30 or above, and mild otherwise.

challenge-1.sql
 
Output
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