Weather Type in Each Country - Problem

Imagine you're working for a global weather analytics company that needs to classify countries based on their November 2019 weather patterns. You have access to two databases:

๐Ÿ“Š Countries Table: Contains country information
๐ŸŒก๏ธ Weather Table: Contains daily weather measurements

Your task is to determine the weather type for each country based on their average weather state during November 2019:

  • โ„๏ธ Cold: Average weather_state โ‰ค 15
  • ๐Ÿ”ฅ Hot: Average weather_state โ‰ฅ 25
  • ๐ŸŒค๏ธ Warm: Everything in between (15 < average < 25)

Input Tables:

Countries
country_id (int)country_name (varchar)
Weather
country_id (int)weather_state (int)day (date)

Goal: Return each country with its weather classification for November 2019.

Input & Output

example_1.sql โ€” Basic Weather Classification
$ Input: Countries: [(1,'USA'), (2,'Canada'), (3,'India')] Weather: [(1,15,'2019-11-01'), (1,25,'2019-11-02'), (2,10,'2019-11-01'), (2,12,'2019-11-02'), (3,30,'2019-11-01'), (3,35,'2019-11-02')]
โ€บ Output: [('USA','Warm'), ('Canada','Cold'), ('India','Hot')]
๐Ÿ’ก Note: USA: avg(15,25) = 20 โ†’ Warm (15 < 20 < 25). Canada: avg(10,12) = 11 โ†’ Cold (11 โ‰ค 15). India: avg(30,35) = 32.5 โ†’ Hot (32.5 โ‰ฅ 25).
example_2.sql โ€” Edge Case Averages
$ Input: Countries: [(1,'Country1'), (2,'Country2')] Weather: [(1,15,'2019-11-01'), (1,15,'2019-11-02'), (2,25,'2019-11-01'), (2,25,'2019-11-02')]
โ€บ Output: [('Country1','Cold'), ('Country2','Hot')]
๐Ÿ’ก Note: Country1: avg(15,15) = 15 โ†’ Cold (exactly 15 โ‰ค 15). Country2: avg(25,25) = 25 โ†’ Hot (exactly 25 โ‰ฅ 25). Boundary values are inclusive.
example_3.sql โ€” Missing November Data
$ Input: Countries: [(1,'USA'), (2,'Canada')] Weather: [(1,20,'2019-10-31'), (1,20,'2019-12-01'), (2,15,'2019-11-01')]
โ€บ Output: [('Canada','Cold')]
๐Ÿ’ก Note: Only Canada has November 2019 data. USA has October and December data but no November data, so it's excluded from results. Only countries with November 2019 weather data appear in output.

Visualization

Tap to expand
CountriesWeather NovJOIN & AVGโ‰ค15: Cold15-25: Warmโ‰ฅ25: HotClassified Results
Understanding the Visualization
1
๐Ÿ“Š Join the Data
Combine country information with their weather measurements, like matching students with their test scores
2
๐Ÿ“… Filter Time Period
Focus only on November 2019 data, like considering only final exam scores
3
๐Ÿ“ˆ Calculate Averages
Group by country and calculate average weather_state, like computing each student's GPA
4
๐Ÿท๏ธ Apply Classification
Use CASE WHEN to assign Cold/Warm/Hot labels based on average ranges, like assigning A/B/C grades based on GPA
Key Takeaway
๐ŸŽฏ Key Insight: SQL's GROUP BY with aggregate functions (AVG) combined with conditional logic (CASE WHEN) allows us to classify data efficiently in a single query, avoiding the need for multiple passes or complex subqueries.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n + m)

Where n is countries, m is weather records. JOIN and GROUP BY operations with index usage

n
2n
โšก Linearithmic
Space Complexity
O(k)

Where k is number of distinct countries, for grouping and result storage

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค Countries table rows โ‰ค 100
  • 1 โ‰ค Weather table rows โ‰ค 1000
  • weather_state values are integers between 1 and 100
  • All dates are in November 2019 format: 2019-11-XX
  • Each country may have multiple weather readings per day
Asked in
Amazon 25 Google 18 Microsoft 15 Meta 12
28.0K Views
Medium Frequency
~8 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen