Match earthquakes to US states
AdvancedJOIN on messy keysEarthquakes (USGS, Oct 2026)
Places end in either a state name (, Alaska) or a code (, CA). Join quakes to state_abbrev to count events per state name, catching both forms. Return state name and count, largest first, then name A→Z.
Solution
SELECT s.name, COUNT(*) AS n
FROM quakes q
JOIN state_abbrev s
ON q.place LIKE '%, ' || s.abbrev
OR q.place LIKE '%, ' || s.name
GROUP BY s.name
ORDER BY n DESC, s.name;Expected output:
Hint
Join ON q.place LIKE '%, ' || s.abbrev OR q.place LIKE '%, ' || s.name.
Same question in Excel / Google Sheets
Helper column with the text after the last comma, map codes to names with XLOOKUP against state_abbrev, then pivot.
Download the CSV from the datasets page. Column letters assume the CSV opens with its header in row 1.
Tip: Ctrl+Enter (⌘+Enter on Mac) checks your answer. Everything runs in your browser; nothing you type is sent anywhere.