Rising Temperature
Asked at Meta
Difficulty
Easy
Time
O(n log n) with an index on reco
Space
O(n)
Pattern
Database
Companies
1
Problem
You are given a table called Weather. Each row has an id, a recordDate (a calendar date), and a temperature reading for that date. No two rows share the same date.
Write a SQL query that returns the id of every day whose temperature is strictly higher than the temperature on the day right before it. "The day before" means exactly one calendar day earlier, not just the previous row in the table.
Return a single column named id. The order of the rows does not matter. Days with no previous day in the table (for example, the earliest date) can never qualify.
Which days beat the day before?
Constraints
- ●
id is unique for each row - ●
recordDate is unique for each row (no two rows share the same date) - ●
temperature is an integer - ●
The table may be empty - ●
Return the result in any order
Examples
Weather table:
+----+------------+-------------+
| id | recordDate | temperature |
+----+------------+-------------+
| 1 | 2015-01-01 | 10 |
| 2 | 2015-01-02 | 25 |
| 3 | 2015-01-+----+
| id |
+----+
| 2 |
| 4 |
+----+Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Meta | Easy | View all Meta questions → |
How to Think About It
Self-join the table on itself, matching each record to the one from the day before.
Use DATE_SUB or interval arithmetic to find records exactly one day apart.
Compare temperatures: w1.temperature > w2.temperature where w2 is the previous day.
Return w1.id for all matching rows.
Alternatively, use window functions with LAG() to access the previous row's temperature.
Optimal Approach
Self-join the Weather table: join w1 with w2 where w2.recordDate = DATE_SUB(w1.recordDate, INTERVAL 1 DAY). Filter rows where w1.temperature > w2.temperature. Return w1.id. This handles the one-day gap requirement and works with MySQL date functions.
Self-join on yesterday's date
What Trips People Up in Real Interviews
Dates are guaranteed to be unique, so a simple join on DATE_SUB(recordDate, INTERVAL 1 DAY) works.
Using LAG() is cleaner but requires knowledge of window functions.
Make sure to use the correct date arithmetic syntax for your SQL dialect.
Ask whether the output column should be named id or temperature.
Corner case: if there is no previous day in the table, that row is simply excluded.
Solution Code
O(n log n) with an index on reco timeSELECT w1.id
FROM Weather w1
JOIN Weather w2
ON w2.recordDate = DATE_SUB(w1.recordDate, INTERVAL 1 DAY)
WHERE w1.temperature > w2.temperature;Frequently Asked Questions
What is the Rising Temperature problem?
Write a SQL query to find all dates where the temperature was higher than the previous day. The table Weather has columns id (int), recordDate (date), and temperature (int). Return the result with column id.
How do you solve Rising Temperature?
The optimal approach runs in O(n log n) with an index on reco time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Rising Temperature?
Rising Temperature is asked at Meta. It is a easy difficulty problem.
What are common mistakes on Rising Temperature?
- Dates are guaranteed to be unique, so a simple join on DATE_SUB(recordDate, INTERVAL 1 DAY) works.
- Using LAG() is cleaner but requires knowledge of window functions.
- Make sure to use the correct date arithmetic syntax for your SQL dialect.
- Ask whether the output column should be named id or temperature.
- Corner case: if there is no previous day in the table, that row is simply excluded.