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

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
7Little RockAR34.725393-92.358556120.281
8Los AngelesCA34.019394-118.410825470.517
9San FranciscoCA37.727239-123.03222946.7
10San DiegoCA32.814977-117.135562326.087
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
city_idnumber · unique id
7
city_nametext
Little Rock
state_codetext · links to states
AR
latitudedecimal
34.725393
longitudedecimal
-92.358556
land_area_sq_midecimal
120.281
city_idnumber · unique id
8
city_nametext
Los Angeles
state_codetext · links to states
CA
latitudedecimal
34.019394
longitudedecimal
-118.410825
land_area_sq_midecimal
470.517
city_idnumber · unique id
9
city_nametext
San Francisco
state_codetext · links to states
CA
latitudedecimal
37.727239
longitudedecimal
-123.032229
land_area_sq_midecimal
46.7
city_idnumber · unique id
10
city_nametext
San Diego
state_codetext · links to states
CA
latitudedecimal
32.814977
longitudedecimal
-117.135562
land_area_sq_midecimal
326.087

first 10 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
USW00004853Cleveland Burke Lakefront Airport64141.5256-81.6692177.42022-01-012024-12-311096
USW00012815Orlando International Airport19128.4183-81.324227.12022-01-012024-12-311096
USW00012839Miami International Airport18125.7881-80.31691.52022-01-012024-12-311096
USW00012842Tampa International Airport20127.9633-82.541.82022-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
station_idtext · unique id
USW00004853
station_nametext
Cleveland Burke Lakefront Airport
city_idnumber · links to cities
64
is_primarynumber
1
latitudedecimal
41.5256
longitudedecimal
-81.6692
elevation_mdecimal
177.4
first_observationdate
2022-01-01
last_observationdate
2024-12-31
reported_daysnumber
1096
station_idtext · unique id
USW00012815
station_nametext
Orlando International Airport
city_idnumber · links to cities
19
is_primarynumber
1
latitudedecimal
28.4183
longitudedecimal
-81.3242
elevation_mdecimal
27.1
first_observationdate
2022-01-01
last_observationdate
2024-12-31
reported_daysnumber
1096
station_idtext · unique id
USW00012839
station_nametext
Miami International Airport
city_idnumber · links to cities
18
is_primarynumber
1
latitudedecimal
25.7881
longitudedecimal
-80.3169
elevation_mdecimal
1.5
first_observationdate
2022-01-01
last_observationdate
2024-12-31
reported_daysnumber
1096
station_idtext · unique id
USW00012842
station_nametext
Tampa International Airport
city_idnumber · links to cities
20
is_primarynumber
1
latitudedecimal
27.9633
longitudedecimal
-82.54
elevation_mdecimal
1.8
first_observationdate
2022-01-01
last_observationdate
2024-12-31
reported_daysnumber
1096

first 10 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
2022-01-072022175winter7Friday01
2022-01-082022186winter8Saturday11
2022-01-092022190winter9Sunday11
2022-01-1020221101winter10Monday01
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
datedate · unique id
2022-01-07
yearnumber
2022
monthnumber
1
day_of_yearnumber
7
day_of_weeknumber
5
meteorological_seasontext
winter
day_of_monthnumber
7
day_nametext
Friday
is_weekendnumber
0
quarternumber
1
datedate · unique id
2022-01-08
yearnumber
2022
monthnumber
1
day_of_yearnumber
8
day_of_weeknumber
6
meteorological_seasontext
winter
day_of_monthnumber
8
day_nametext
Saturday
is_weekendnumber
1
quarternumber
1
datedate · unique id
2022-01-09
yearnumber
2022
monthnumber
1
day_of_yearnumber
9
day_of_weeknumber
0
meteorological_seasontext
winter
day_of_monthnumber
9
day_nametext
Sunday
is_weekendnumber
1
quarternumber
1
datedate · unique id
2022-01-10
yearnumber
2022
monthnumber
1
day_of_yearnumber
10
day_of_weeknumber
1
meteorological_seasontext
winter
day_of_monthnumber
10
day_nametext
Monday
is_weekendnumber
0
quarternumber
1

first 10 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
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. 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

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

Return station_id and date.

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