197. Rising Temperature
- User
- Rising Temperature
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To solve the problem of identifying dates where the temperature was higher than the previous day, we can use a self-join on the Weather table. This allows us to compare each day's temperature with the temperature of the previous day.
We are given a table Weather with the following structure:
| Column | Type |
|---|---|
| Id | INT |
| Date | DATE |
| Temperature | INT |
Our goal is to return the Id values of all dates where the temperature was higher than the previous day's temperature.
To compare each day's temperature with the previous day's, we can:
Weather table with itself (w1 for current day, w2 for previous day).w1.Date is exactly one day after w2.Date.w1.Temperature > w2.Temperature.This ensures that we're only comparing consecutive days and only including those where the temperature increased.
SELECT w1.Id
FROM Weather w1
JOIN Weather w2
ON w1.Date = DATE_ADD(w2.Date, INTERVAL 1 DAY)
WHERE w1.Temperature > w2.Temperature;
JOIN Weather w2: We join the same table again to get the previous day's data.ON w1.Date = DATE_ADD(w2.Date, INTERVAL 1 DAY): This ensures that w1 is the current day and w2 is the previous day.WHERE w1.Temperature > w2.Temperature: We filter to only include days where the temperature increased from the previous day.Given the following data:
| Id | Date | Temperature |
|---|---|---|
| 1 | 2023-01-01 | 10 |
| 2 | 2023-01-02 | 25 |
| 3 | 2023-01-03 | 20 |
| 4 | 2023-01-04 | 30 |
The query would return:
| Id |
|---|
| 2 |
| 4 |
Explanation:
DATE_ADD ensures compatibility with MySQL, which is the expected SQL dialect for this problem.SELECT w1.Id
FROM Weather w1
JOIN Weather w2
ON w1.Date = DATE_ADD(w2.Date, INTERVAL 1 DAY)
WHERE w1.Temperature > w2.Temperature;