runtime
23 / 88

lesson 23 Joins & sets · intermediate

CROSS JOIN & a complete date grid

Build every combination of dates and entities.

the idea

CROSS JOIN pairs every row of one table with every row of another. Pairing stations with calendar dates builds a complete grid of expected station-days; a LEFT JOIN from that grid to the observations then shows which expected days have no data.

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.

cities6 columns · 96 rows
city_idnumber · unique idcity_nametextstate_codetext · links to stateslatitudedecimallongitudedecimalland_area_sq_midecimal
1BirminghamAL33.527174-86.797036147.07
2MobileAL30.668426-88.100226139.482
3AnchorageAK61.17425-149.2843291707.004
4FairbanksAK64.836531-147.65174531.746
5PhoenixAZ33.572154-112.090132518.331
6TucsonAZ32.153036-110.870773242.187
city_idnumber · unique id
1
city_nametext
Birmingham
state_codetext · links to states
AL
latitudedecimal
33.527174
longitudedecimal
-86.797036
land_area_sq_midecimal
147.07
city_idnumber · unique id
2
city_nametext
Mobile
state_codetext · links to states
AL
latitudedecimal
30.668426
longitudedecimal
-88.100226
land_area_sq_midecimal
139.482
city_idnumber · unique id
3
city_nametext
Anchorage
state_codetext · links to states
AK
latitudedecimal
61.17425
longitudedecimal
-149.284329
land_area_sq_midecimal
1707.004
city_idnumber · unique id
4
city_nametext
Fairbanks
state_codetext · links to states
AK
latitudedecimal
64.836531
longitudedecimal
-147.651745
land_area_sq_midecimal
31.746
city_idnumber · unique id
5
city_nametext
Phoenix
state_codetext · links to states
AZ
latitudedecimal
33.572154
longitudedecimal
-112.090132
land_area_sq_midecimal
518.331
city_idnumber · unique id
6
city_nametext
Tucson
state_codetext · links to states
AZ
latitudedecimal
32.153036
longitudedecimal
-110.870773
land_area_sq_midecimal
242.187

first 6 of 96 rows · your queries use all of them

stations10 columns · 100 rows
station_idtext · unique idstation_nametextcity_idnumber · links to citiesis_primarynumberlatitudedecimallongitudedecimalelevation_mdecimalfirst_observationdatelast_observationdatereported_daysnumber
USW00003131San Diego Montgomery Fld10132.8144-117.1375127.42022-01-012024-12-311096
USW00003184Phoenix Deer Valley Muni Airport5133.6903-112.0661453.22022-01-012024-12-311096
USW00003822Savannah International Airport24132.1314-81.20228.82022-01-012024-12-311096
USW00003888Atlanta Fulton Co Airport23133.7775-84.5247245.12022-01-012024-12-311095
USW00003928Wichita32137.6544-97.4431426.72022-01-012024-12-311096
USW00003960Cahokia St Louis Dwtn Airport45138.5639-90.1486123.42022-01-012024-12-311096
station_idtext · unique id
USW00003131
station_nametext
San Diego Montgomery Fld
city_idnumber · links to cities
10
is_primarynumber
1
latitudedecimal
32.8144
longitudedecimal
-117.1375
elevation_mdecimal
127.4
first_observationdate
2022-01-01
last_observationdate
2024-12-31
reported_daysnumber
1096
station_idtext · unique id
USW00003184
station_nametext
Phoenix Deer Valley Muni Airport
city_idnumber · links to cities
5
is_primarynumber
1
latitudedecimal
33.6903
longitudedecimal
-112.0661
elevation_mdecimal
453.2
first_observationdate
2022-01-01
last_observationdate
2024-12-31
reported_daysnumber
1096
station_idtext · unique id
USW00003822
station_nametext
Savannah International Airport
city_idnumber · links to cities
24
is_primarynumber
1
latitudedecimal
32.1314
longitudedecimal
-81.2022
elevation_mdecimal
8.8
first_observationdate
2022-01-01
last_observationdate
2024-12-31
reported_daysnumber
1096
station_idtext · unique id
USW00003888
station_nametext
Atlanta Fulton Co Airport
city_idnumber · links to cities
23
is_primarynumber
1
latitudedecimal
33.7775
longitudedecimal
-84.5247
elevation_mdecimal
245.1
first_observationdate
2022-01-01
last_observationdate
2024-12-31
reported_daysnumber
1095
station_idtext · unique id
USW00003928
station_nametext
Wichita
city_idnumber · links to cities
32
is_primarynumber
1
latitudedecimal
37.6544
longitudedecimal
-97.4431
elevation_mdecimal
426.7
first_observationdate
2022-01-01
last_observationdate
2024-12-31
reported_daysnumber
1096
station_idtext · unique id
USW00003960
station_nametext
Cahokia St Louis Dwtn Airport
city_idnumber · links to cities
45
is_primarynumber
1
latitudedecimal
38.5639
longitudedecimal
-90.1486
elevation_mdecimal
123.4
first_observationdate
2022-01-01
last_observationdate
2024-12-31
reported_daysnumber
1096

first 6 of 100 rows · your queries use all of them

calendar10 columns · 1,096 rows
datedate · unique idyearnumbermonthnumberday_of_yearnumberday_of_weeknumbermeteorological_seasontextday_of_monthnumberday_nametextis_weekendnumberquarternumber
2022-01-012022116winter1Saturday11
2022-01-022022120winter2Sunday11
2022-01-032022131winter3Monday01
2022-01-042022142winter4Tuesday01
2022-01-052022153winter5Wednesday01
2022-01-062022164winter6Thursday01
datedate · unique id
2022-01-01
yearnumber
2022
monthnumber
1
day_of_yearnumber
1
day_of_weeknumber
6
meteorological_seasontext
winter
day_of_monthnumber
1
day_nametext
Saturday
is_weekendnumber
1
quarternumber
1
datedate · unique id
2022-01-02
yearnumber
2022
monthnumber
1
day_of_yearnumber
2
day_of_weeknumber
0
meteorological_seasontext
winter
day_of_monthnumber
2
day_nametext
Sunday
is_weekendnumber
1
quarternumber
1
datedate · unique id
2022-01-03
yearnumber
2022
monthnumber
1
day_of_yearnumber
3
day_of_weeknumber
1
meteorological_seasontext
winter
day_of_monthnumber
3
day_nametext
Monday
is_weekendnumber
0
quarternumber
1
datedate · unique id
2022-01-04
yearnumber
2022
monthnumber
1
day_of_yearnumber
4
day_of_weeknumber
2
meteorological_seasontext
winter
day_of_monthnumber
4
day_nametext
Tuesday
is_weekendnumber
0
quarternumber
1
datedate · unique id
2022-01-05
yearnumber
2022
monthnumber
1
day_of_yearnumber
5
day_of_weeknumber
3
meteorological_seasontext
winter
day_of_monthnumber
5
day_nametext
Wednesday
is_weekendnumber
0
quarternumber
1
datedate · unique id
2022-01-06
yearnumber
2022
monthnumber
1
day_of_yearnumber
6
day_of_weeknumber
4
meteorological_seasontext
winter
day_of_monthnumber
6
day_nametext
Thursday
is_weekendnumber
0
quarternumber
1

first 6 of 1,096 rows · your queries use all of them

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. Pair every date with one station. Run this. CROSS JOIN builds every combination of station and calendar date in the filter.the pattern

    example.sql
    SELECT s.station_id, c.date
    FROM stations s
    CROSS JOIN calendar c
    WHERE s.station_id = 'USW00094728'
      AND c.date BETWEEN '2024-01-01' AND '2024-01-03';
  2. Compare expected days to reported days. Run this. LEFT JOIN observations onto the grid; COUNT(d.observation_date) counts days with data.

    step-2.sql
    SELECT
      s.station_id,
      COUNT(*) AS expected_days,
      COUNT(d.observation_date) AS reported_days
    FROM stations s
    CROSS JOIN calendar c
    LEFT JOIN daily_weather d
      ON d.station_id = s.station_id
      AND d.observation_date = c.date
    GROUP BY s.station_id
    ORDER BY reported_days
    LIMIT 5;

common mistake

Grouping or averaging only the observations hides missing days entirely: a station that did not report looks the same as one that did.

challenge 1

Return station_id and date for every station in a Pennsylvania city (cities.state_code = 'PA') and every calendar date from 2024-11-15 to 2024-11-21 inclusive.

what to return

Columns
station_iddate 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.
CROSS JOIN & a complete date grid — Joins & sets · Runtime