CMD Guide
HomeDatabasesSQL Practice Problems

Weather Type in Each Country

Mechanism

The engine joins each Weather row to its Countries row on country_id, keeps only the November-2019 rows, collapses the survivors into one group per country with GROUP BY, computes AVG(weather_state) over each group, and a CASE maps that single average onto a Cold/Warm/Hot label. The whole query is one AVG per country wrapped in a three-way threshold test.

The correct query

SELECT c.country_name,
       CASE WHEN AVG(w.weather_state) <= 15 THEN 'Cold'
            WHEN AVG(w.weather_state) >= 25 THEN 'Hot'
            ELSE 'Warm'
       END AS weather_type
FROM   Countries c
JOIN   Weather w ON w.country_id = c.country_id
WHERE  w.day >= '2019-11-01' AND w.day < '2019-12-01'
GROUP BY c.country_id, c.country_name;

Two deliberate choices beyond the original answer. First, the date filter lives in WHERE, not in the join's ON — for this INNER JOIN both produce the same rows, but the placement encodes intent and survives a later change to LEFT JOIN (the pitfall below). Second, the predicate is a half-open range day >= '2019-11-01' AND day < '2019-12-01' rather than YEAR(day)=2019 AND MONTH(day)=11. Wrapping the column in YEAR()/MONTH() makes the predicate non-sargable: the engine must call the function on every row and cannot seek an index on day. A bare-column range comparison can use a B-tree index on day. Group by country_id (the key) and carry country_name along; grouping by name alone breaks if two countries share a name.

Why the naive trace is misleading

The original walkthrough invents tables called Step1, Step2, Step3 and selects FROM Step1. Those tables do not exist — you cannot run those snippets. SQL has no such intermediate relations you can name; the optimizer produces internal pipeline stages, not queryable tables. The trace below uses the real evaluation order instead.

Worked trace on the real rows

The logical clause order is FROM/JOIN → WHERE → GROUP BY → aggregate → SELECT/CASE. Every November row already passes the date filter, so after the join + WHERE the engine groups by country and averages:

country_idweather_state values (Nov 2019)sum / countAVGtestlabel
21515 / 115.000≤ 15Cold
3-2, 0, 31 / 30.333≤ 15Cold
516, 18, 2155 / 318.33315 < x < 25Warm
72525 / 125.000≥ 25Hot
825, 27, 3183 / 327.667≥ 25Hot

Note the boundaries: 15 hits the <= 15 arm (Cold, not Warm) and 25 hits >= 25 (Hot). The CASE is evaluated top-to-bottom and stops at the first true arm, so an average of exactly 15 can never reach the Warm ELSE. Joining country_id back to names gives USA=Cold, Australia=Cold, China=Warm, Peru=Hot, Morocco=Hot.

diagram
diagram

Pitfalls

Takeaways


Based on LeetCode 1294 “Weather Type in Each Country” and its editorial. The ON-vs-WHERE / outer-join semantics follow the SQL standard's logical clause-evaluation order (FROM/ON → JOIN NULL-padding → WHERE → GROUP BY → SELECT) as described in PostgreSQL and MySQL documentation and in Markus Winand's Use The Index, Luke on sargable predicates. Re-authored and deepened for this guide: corrected the imaginary Step1/Step2/Step3 tables into a real evaluation trace, moved the date filter to a sargable WHERE range, and added the ON-vs-WHERE systems lesson the original omitted.

🤖 Don't fully get this? Learn it with Claude

Stuck on Weather Type in Each Country? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Weather Type in Each Country** (Databases) and want to truly understand it. Explain Weather Type in Each Country from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Weather Type in Each Country** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Weather Type in Each Country** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Weather Type in Each Country** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes