Table: Weather
+-------------+------+ | Column Name | Type | +-------------+------+ | city_id | int | | day | date | | degree | int | +-------------+------+ (city_id, day) is the primary key (combination of columns with unique values) for this table. Each row in this table contains the degree of the weather of a city on a certain day. All the degrees are recorded in the year 2022.
Write a solution to report the day that has the maximum recorded degree in each city. If the maximum degree was recorded for the same city multiple times, return the earliest day among them.
Return the result table ordered by city_id in ascending order.
The result format is shown in the following example.
Example 1:
Input: Weather table: +---------+------------+--------+ | city_id | day | degree | +---------+------------+--------+ | 1 | 2022-01-07 | -12 | | 1 | 2022-03-07 | 5 | | 1 | 2022-07-07 | 24 | | 2 | 2022-08-07 | 37 | | 2 | 2022-08-17 | 37 | | 3 | 2022-02-07 | -7 | | 3 | 2022-12-07 | -6 | +---------+------------+--------+ Output: +---------+------------+--------+ | city_id | day | degree | +---------+------------+--------+ | 1 | 2022-07-07 | 24 | | 2 | 2022-08-07 | 37 | | 3 | 2022-12-07 | -6 | +---------+------------+--------+ Explanation: For city 1, the maximum degree was recorded on 2022-07-07 with 24 degrees. For city 2, the maximum degree was recorded on 2022-08-07 and 2022-08-17 with 37 degrees. We choose the earlier date (2022-08-07). For city 3, the maximum degree was recorded on 2022-12-07 with -6 degrees.
Problem Overview: Each city has multiple temperature records across different days. For every city, return the first day when the maximum recorded degree occurred. If the maximum temperature appears multiple times, the earliest date should be returned.
Approach 1: Aggregation + Join (O(n) time, O(c) space)
Start by computing the maximum recorded degree for each city using GROUP BY city_id with MAX(degree). This produces a small result set containing one row per city with its peak temperature. Next, join this result back to the original table on both city_id and the matching maximum degree. Multiple rows may still exist if the same maximum degree appears on different days. To enforce the “first day” requirement, apply MIN(day) grouped by city_id. This approach relies on standard SQL operations—aggregation, join, and filtering—making it portable across most relational databases. The table scan for aggregation is O(n), and the grouped result size is proportional to the number of cities c, giving O(c) auxiliary space.
Approach 2: Window Function Filtering (O(n) time, O(n) space)
Another clean solution uses SQL window functions. Compute the maximum temperature per city with MAX(degree) OVER (PARTITION BY city_id). This annotates every row with the city’s maximum value without collapsing rows. Filter rows where degree = max_degree, leaving only records that match the peak temperature. Finally, select the earliest day using MIN(day) grouped by city_id. Window functions avoid a separate aggregation join and are often easier to read when supported by the database engine. The scan still processes each record once, giving O(n) time complexity, while storing window computation results leads to roughly O(n) space.
Both solutions rely on core SQL concepts such as GROUP BY, SQL joins, and window functions. The key insight is separating two concerns: identifying the maximum temperature per city, and selecting the earliest occurrence of that maximum.
Recommended for interviews: The aggregation + join approach is the most commonly expected answer. It shows that you understand grouping, joining derived tables, and resolving ties using MIN. The window function version is equally efficient but depends on familiarity with analytic functions, which some interviewers treat as an advanced SQL technique.
MySQL
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Aggregation + Join | O(n) | O(c) | Most common SQL interview pattern when computing group maxima then filtering matching rows |
| Window Function + Filter | O(n) | O(n) | When the database supports analytic functions and you want a concise query without a self join |
LeetCode Medium 2314 "First Day of Maximum Recorded Degree" Interview SQL Question with Explanation • Everyday Data Science • 1,504 views views
Practice The First Day of the Maximum Recorded Degree in Each City with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor