Given a Weather table, write a SQL query to find all dates’ Ids with higher temperature compared to its previous (yesterday’s) dates.
+---------+------------+------------------+ | Id(INT) | Date(DATE) | Temperature(INT) | +---------+------------+------------------+ | 1 | 2015-01-01 | 10 | | 2 | 2015-01-02 | 25 | | 3 | 2015-01-03 | 20 | | 4 | 2015-01-04 | 30 | +---------+------------+------------------+
For example, return the following Ids for the above Weather table:
+----+ | Id | +----+ | 2 | | 4 | +----+
The SQL puzzle is classified as ‘Easy’ in Oj Online Judge: https://leetcode.com/problems/rising-temperature/
The solution is to select from the table two times and compare the dates and temperature:
select A.Id from Weather as A, Weather as B where A.Temperature > B.Temperature and TO_DAYS(A.Date) = TO_DAYS(B.Date) + 1
Of course, you could also use “INTERVAL 1 DAY” to accomplish the same task.
select A.Id from Weather as A, Weather as B where A.Temperature > B.Temperature and A.Date = B.Date + INTERVAL 1 DAY
–EOF (The Ultimate Computing & Technology Blog) —
Last Post: Linux Bash Coding Exercise - Get the Tenth Line of File
Next Post: SQL Coding Exercise - Delete Duplicate Emails