Find days with a higher temperature than the previous calendar day
Quick Overview
Write a SQL query that returns the id of every day whose temperature is strictly higher than the reading on the previous calendar day, skipping dates that have no reading for yesterday. It tests self-joins, date arithmetic and handling gaps in daily data.
Find days with a higher temperature than the previous calendar day
Company: Microsoft
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
The `Weather` table stores one temperature reading per date. Return the `id` of every date whose temperature is strictly higher than the temperature on the previous calendar day (yesterday). A date with no record for yesterday is excluded.
```hint Define yesterday by the date
Find each date's comparison row through date arithmetic on `recordDate`, then check what your query does with a date whose yesterday is missing.
```
### Input Table(s)
Table `Weather`
| Column | Type | Meaning |
|---|---|---|
| id | INT | Row identifier |
| recordDate | DATE | The date of the reading |
| temperature | INT | The temperature recorded on that date |
Keys: `id` is the primary key. No two rows share the same `recordDate`.
### Output Contract
- Exactly one column, `Id`, holding the `id` of each qualifying row.
- One row per qualifying date.
- Sort the result by `Id` ascending.
- No rounding is involved; `Id` is an integer.
### Example
Input `Weather`:
| id | recordDate | temperature |
|---|---|---|
| 1 | 2023-06-10 | 30 |
| 2 | 2023-06-11 | 30 |
| 3 | 2023-06-12 | 35 |
| 4 | 2023-06-14 | 40 |
Expected output:
| Id |
|---|
| 3 |
Row 3 qualifies because 35 is higher than the 30 recorded on 2023-06-11. Row 2 ties with the day before it, so it is not strictly higher. Row 4 is warmer than row 3, but there is no row for 2023-06-13, so it has no yesterday and is excluded. Row 1 is the earliest date and has nothing to compare against.
### Constraints and Clarifications
- `Weather` has at most 10,000 rows.
- `recordDate` is unique, so each date has at most one reading and at most one yesterday.
- Yesterday means exactly one calendar day earlier. When there is a gap in the dates, the previous row in date order does not count.
- An equal temperature does not qualify; the comparison is strict.
- If the temperature on either day is NULL, the date does not qualify.
- The schema does not promise that `id` follows date order, so `id - 1` is not a way to find yesterday.
Overview: Write a SQL query that returns the id of every day whose temperature is strictly higher than the reading on the previous calendar day, skipping dates that have no reading for yesterday. It tests self-joins, date arithmetic and handling gaps in daily data.
The Weather table stores one temperature reading per date. Return the id of every date whose temperature is strictly higher than the temperature on the previous calendar day (yesterday). A date with no record for yesterday is excluded.
Input Table(s)
Table Weather
Column
Type
Meaning
id
INT
Row identifier
recordDate
DATE
The date of the reading
temperature
INT
The temperature recorded on that date
Keys: id is the primary key. No two rows share the same recordDate.
Output Contract
Exactly one column,
Id
, holding the
id
of each qualifying row.
One row per qualifying date.
Sort the result by
Id
ascending.
No rounding is involved;
Id
is an integer.
Example
Input Weather:
id
recordDate
temperature
1
2023-06-10
30
2
2023-06-11
30
3
2023-06-12
35
4
2023-06-14
40
Expected output:
Id
3
Row 3 qualifies because 35 is higher than the 30 recorded on 2023-06-11. Row 2 ties with the day before it, so it is not strictly higher. Row 4 is warmer than row 3, but there is no row for 2023-06-13, so it has no yesterday and is excluded. Row 1 is the earliest date and has nothing to compare against.
Constraints and Clarifications
Weather
has at most 10,000 rows.
recordDate
is unique, so each date has at most one reading and at most one yesterday.
Yesterday means exactly one calendar day earlier. When there is a gap in the dates, the previous row in date order does not count.
An equal temperature does not qualify; the comparison is strict.
If the temperature on either day is NULL, the date does not qualify.
The schema does not promise that
id
follows date order, so
id - 1
is not a way to find yesterday.