Rising Temperature
Write an SQL query to find all dates' `id` with higher temperatures compared to its previous dates (yesterday). Return the result table in **any order**. ### Table: Weather | Column Name | Type | | :--- | :--- | | id | int | | recordDate | date | | temperature | int | `id` is the primary key column for this table. Each row contains information about the temperature on a given day. ### Example: **Input:** ```text Weather table: +----+------------+-------------+ | id | recordDate | temperature | +----+------------+-------------+ | 1 | 2015-01-01 | 10 | | 2 | 2015-01-02 | 25 | | 3 | 2015-01-03 | 20 | | 4 | 2015-01-04 | 30 | +----+------------+-------------+ ``` **Output:** ```text +----+ | id | +----+ | 2 | | 4 | +----+ ```
Solve It in the BrowserPractice Explaining It Out Loud
Optimal Approach
- Time complexity: O(N^2) or O(N)
- Space complexity: O(N)
With an index on recordDate, joining yesterday rows evaluates in O(N) time.
Follow-Up Questions Interviewers Ask About Rising Temperature
- Why does date(w1.recordDate, "-1 day") work across SQL dialects?
SQLite supports date(col, "-1 day"). In PostgreSQL, you would use w1.recordDate - INTERVAL "1 day", and in MySQL, SUBDATE(w1.recordDate, 1). - Could LAG() be used here?
LAG(temperature) OVER (ORDER BY recordDate) works only if all dates are strictly consecutive without missing calendar days. A self-join with explicit date arithmetic handles missing dates correctly.
Practice the full problem with execution, tests, and AI code review in the interactive lab — then rehearse explaining your solution in a live voice mock interview.