Description
Table: Countries
| Column Name | Type |
|---|---|
| country_id | int |
| country_name | varchar |
country_idis the primary key (column with unique values) for this table.- Each row of this table contains the ID and the name of one country.
Table: Weather
| Column Name | Type |
|---|---|
| country_id | int |
| weather_state | int |
| day | date |
(country_id, day)is the primary key (combination of columns with unique values) for this table.- Each row of this table indicates the weather state in a country for one day.
Problem Statement
Write a solution to find the type of weather in each country for November 2019.
The type of weather is:
- ‘Cold’ if the average
weather_stateis less than or equal 15, - ‘Hot’ if the average
weather_stateis greater than or equal to 25, and ‘Warm’ otherwise. Return the result table in any order.
The result format is in the following example.
Example 1:
Input:
Countriestable:
| country_id | country_name |
|---|---|
| 2 | USA |
| 3 | Australia |
| 7 | Peru |
| 5 | China |
| 8 | Morocco |
| 9 | Spain |
Weathertable:
| country_id | weather_state | day |
|---|---|---|
| 2 | 15 | 2019-11-01 |
| 2 | 12 | 2019-10-28 |
| 2 | 12 | 2019-10-27 |
| 3 | -2 | 2019-11-10 |
| 3 | 0 | 2019-11-11 |
| 3 | 3 | 2019-11-12 |
| 5 | 16 | 2019-11-07 |
| 5 | 18 | 2019-11-09 |
| 5 | 21 | 2019-11-23 |
| 7 | 25 | 2019-11-28 |
| 7 | 22 | 2019-12-01 |
| 7 | 20 | 2019-12-02 |
| 8 | 25 | 2019-11-05 |
| 8 | 27 | 2019-11-15 |
| 8 | 31 | 2019-11-25 |
| 9 | 7 | 2019-10-23 |
| 9 | 3 | 2019-12-23 |
Output:
| country_name | weather_type |
|---|---|
| USA | Cold |
| Australia | Cold |
| Peru | Hot |
| Morocco | Hot |
| China | Warm |
Explanation:
- Average
weather_statein USA in November is(15) / 1 = 15so weather type is Cold. - Average
weather_statein Austraila in November is(-2 + 0 + 3) / 3 = 0.333so weather type is Cold. - Average
weather_statein Peru in November is(25) / 1 = 25so the weather type is Hot. - Average
weather_statein China in November is(16 + 18 + 21) / 3 = 18.333so weather type is Warm. - Average
weather_statein Morocco in November is(25 + 27 + 31) / 3 = 27.667so weather type is Hot. - We know nothing about the average
weather_statein Spain in November so we do not include it in the result table.
Solution
In this problem, the output requires us to return country_name with average weather in string format based on criteria defined in the problem. So, we need information from both tables Weather and Countries. Therefore, we do need to join by country_id field.
We also need to filter the weather results for 2019-11 which you can do using various methods but I have used DATE_FORMAT(day, '%Y-%m') = '2019-11'. Once you perform these two things, you can have a query like this.
1SELECT c.country_name, w.weather_state
2 FROM Weather w
3 JOIN Countries c
4 ON c.country_id = w.country_id
5 WHERE DATE_FORMAT(w.day, '%Y-%m') = '2019-11'
This query returns all rows where date is from November 2019.
The next task is to find the average temperature by country. This is a grouping operation with AVG() function. Once you’ve found the average per country for November 2019, you need to compare that weather_state with given conditions to define the weather in three categories as Hot, Warm or Cold. This can be done using CASE WHEN clause like this.
1CASE
2 WHEN AVG(w.weather_state) <= 15 THEN 'Cold'
3 WHEN AVG(w.weather_state) >= 25 THEN 'Hot'
4 ELSE 'Warm' END AS weather_type
The final solution looks like this.
1SELECT
2 c.country_name,
3 CASE
4 WHEN AVG(w.weather_state) <= 15 THEN 'Cold'
5 WHEN AVG(w.weather_state) >= 25 THEN 'Hot'
6 ELSE 'Warm' END AS weather_type
7 FROM Weather w
8 JOIN Countries c
9 ON c.country_id = w.country_id
10 WHERE DATE_FORMAT(w.day, '%Y-%m') = '2019-11'
11 GROUP BY c.country_name;


Comments