EasyDatabaseUpdated Sep 2026

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?

WeatheridrecordDatetemperature012312015-01-011022015-01-022532015-01-032042015-01-0430Rows 2 and 4 are warmer than the day before, so theirids (2 and 4) are the answer.
Compare each day with the day exactly one calendar day earlier, and keep the id only when the temperature went up.

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

Example 1
InputWeather table: +----+------------+-------------+ | id | recordDate | temperature | +----+------------+-------------+ | 1 | 2015-01-01 | 10 | | 2 | 2015-01-02 | 25 | | 3 | 2015-01-
Output+----+ | id | +----+ | 2 | | 4 | +----+
Whyid 2 is 2015-01-02 with 25, and the day before (2015-01-01) was 10, so 25 > 10 and it qualifies. id 3 is 20 versus 25 the day before, which is lower, so it is skipped. id 4 is 30 versus 20 the day before, so it qualifies. id 1 has no previous day, so it never qualifies.

Asked At

1 companies
CompanyDifficulty
MetaEasyView all Meta questions →

How to Think About It

1

Self-join the table on itself, matching each record to the one from the day before.

2

Use DATE_SUB or interval arithmetic to find records exactly one day apart.

3

Compare temperatures: w1.temperature > w2.temperature where w2 is the previous day.

4

Return w1.id for all matching rows.

5

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

self-join Weather w1, Weather w2match w2.recordDate = w1.recordDate - 1 daykeep rows where w1.temperature > w2.temperatreturn w1.idThe join finds the previous day; the WHERE clause keeps onlythe warmer days.matched pairsidtodayyesterdaykept?ididid22510yes32025no43020yesOnly the pairs where today is greater than yesterday survivethe filter.ON w2.recordDate = DATE_SUB(w1.recordDate, INTERVAL 1 DAY)WHERE w1.temperature > w2.temperatureSELECT w1.id
Join each row to the row one day earlier, then keep only the rows where today is hotter.
ComplexityTime O(n log n) with an index on recoSpace O(n)The self-join pairs each row with the row one day earlier; a date index makes that lookup fast, and the join output can hold up to n rows.

What Trips People Up in Real Interviews

1

Dates are guaranteed to be unique, so a simple join on DATE_SUB(recordDate, INTERVAL 1 DAY) works.

2

Using LAG() is cleaner but requires knowledge of window functions.

3

Make sure to use the correct date arithmetic syntax for your SQL dialect.

4

Ask whether the output column should be named id or temperature.

5

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 time
SELECT w1.id
FROM Weather w1
JOIN Weather w2
  ON w2.recordDate = DATE_SUB(w1.recordDate, INTERVAL 1 DAY)
WHERE w1.temperature > w2.temperature;

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

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.