inblog logo
|
stupefyee
    SQL문제풀기

    [SQL 문제 풀이] Rising Temperature (기온 상승)

    Stupefyee's avatar
    Stupefyee
    Jun 05, 2025
    [SQL 문제 풀이] Rising Temperature (기온 상승)
    Contents
    내가 작성한 쿼리
    Rising Temperature - LeetCode
    Can you solve this real interview question? Rising Temperature - Table: Weather +---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | recordDate | date | | temperature | int | +---------------+---------+ id is the column with unique values for this table. There are no different rows with the same recordDate. This table contains information about the temperature on a certain day.   Write a solution to find all dates' id with higher temperatures compared to its previous dates (yesterday). Return the result table in any order. The result format is in the following example.   Example 1: Input: 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: +----+ | id | +----+ | 2 | | 4 | +----+ Explanation: In 2015-01-02, the temperature was higher than the previous day (10 -> 25). In 2015-01-04, the temperature was higher than the previous day (20 -> 30).
    Rising Temperature - LeetCode
    https://leetcode.com/problems/rising-temperature/description/
    Rising Temperature - LeetCode
    notion image
    이전 날짜(어제)에 비해 온도가 높은 모든 날짜의 ID를 찾기 위한 솔루션을 작성하세요. 결과 테이블을 순서에 상관없이 반환하세요.
     

    내가 작성한 쿼리

    Oracle

    SELECT W1.id FROM Weather W1 JOIN Weather W2 ON W1.recordDate = W2.recordDate + 1 -- 날짜 차이가 1만큼 차이나면 join WHERE W1.temperature > W2.temperature;

    MySQL

    SELECT W1.id FROM Weather W1 JOIN Weather W2 ON DATEDIFF(W1.recordDate, W2.recordDate) = 1 -- 날짜 차이가 1만큼 차이나면 join WHERE W1.temperature > W2.temperature; -- 기온 상승한 것만

    차이점

    • 날짜 차이 처리 방식
    Share article
    Contents
    내가 작성한 쿼리

    stupefyee

    RSS·Powered by Inblog