Which single station name should represent an entire state?
Aggregates can be sorted by alias
SELECT state,COUNT(*) AS station_countFROM stationsWHERE state ISNOTNULLGROUPBY stateORDERBY station_count DESC, state;
Sorting turns a summary into a ranking.
Checkpoint 2: summarize states
For every known state, return its station count and mean known elevation. Show the states with the most stations first.
Start with:
SELECT state,FROM stationsWHEREGROUPBYORDERBY
Checkpoint 2: one solution
SELECT state,COUNT(*) AS station_count,ROUND(AVG(elevation)::numeric, 1) AS mean_elevation_mFROM stationsWHERE state ISNOTNULLGROUPBY stateORDERBY station_count DESC, state;
SELECT state, COUNT(*) AS high_station_countFROM stationsWHERE elevation >=500GROUPBY state;
This counts only stations surviving the row-level condition.
HAVING filters completed groups
SELECT state, COUNT(*) AS station_countFROM stationsWHERE state ISNOTNULLGROUPBY stateHAVINGCOUNT(*) >=20;
HAVING asks whether the completed group qualifies.
Read the logical pipeline
FROM choose source rowsWHERE remove individual rowsGROUP BY form groupsHAVING remove completed groupsSELECT calculate displayed valuesORDER BY arrange the resultLIMIT keep a requested number
This is a reasoning order, not the written clause order.
Percentages expose the denominator
SELECT state,COUNT(*) AS station_count,ROUND(100.0*COUNT(elevation) /COUNT(*), 1) AS pct_elevation_knownFROM stationsWHERE state ISNOTNULLGROUPBY state;
100.0 keeps the division from becoming integer arithmetic.
Checkpoint 3: find weak coverage
Among states with at least 20 station records, find the lowest percentage with known elevation.
Return the state, total station count, known-elevation count, and percentage. Sort lowest percentage first.
Checkpoint 3: one solution
SELECT state,COUNT(*) AS station_count,COUNT(elevation) AS known_elevations,ROUND(100.0*COUNT(elevation) /COUNT(*), 1) AS pct_knownFROM stationsWHERE state ISNOTNULLGROUPBY stateHAVINGCOUNT(*) >=20ORDERBY pct_known, state;
A mean also has a denominator
AVG(elevation)
means:
sum of known elevations───────────────────────count of known elevations
It does not use all station rows when elevation is missing.
Correct SQL does not guarantee an honest claim
“State A has better coverage” may mislead because:
station count ignores land area,
active dates may differ,
missingness may be systematic,
observation counts are not station counts.
Project transfer
Choose one project table and produce a group-level quality summary.
Include:
a stated input and output grain,
one count of all rows and one count of known values,
a HAVING condition justified in plain language.
Homework starts here
Save today’s project-transfer query as the first draft of Assignment 1.
Add comments answering:
What does one input row represent?
What does one output row represent?
What is the denominator?
Which records are excluded?
The pattern to keep
define the grain → filter rows → form groups → calculate summaries → filter groups → inspect the denominator → make a defensible claim
Next: connect station facts to observation records with joins.