The rising Temperature question is asked in many companies some of the big names are Google, Facebook, etc.
Lets have a table named weather containing data. Here is the SQL Schema:
CREATE TABLE Weather (id int, recordDate date, temperature int)
INSERT INTO Weather (id, recordDate, temperature) values ('1', '2015-01-01', '10')
INSERT INTO Weather (id, recordDate, temperature) values ('2', '2015-01-02', '25')
INSERT INTO Weather (id, recordDate, temperature) values ('3', '2015-01-03', '20')
INSERT INTO Weather (id, recordDate, temperature) values ('4', '2015-01-04', '30')
Weather table will look like this:
id | recordDate | temperature |
---|---|---|
1 | 2015-01-01 | 10 |
2 | 2015-01-02 | 25 |
3 | 2015-01-03 | 20 |
4 | 2015-01-04 | 30 |
Question: Write an SQL query to find all dates' Id with higher temperatures compared to its previous dates (yesterday).
Output based on the weather table:
id |
---|
2 |
4 |
Here's the Alogorithm,
We will be using DATEDIFF function to compare the difference between dates.
Query to produce the required output:
SELECT w.id FROM Weather w JOIN Weather w2 WHERE w.temperature>w2.temperature AND datediff(w.recordDate,w2.recordDate)=1;
I hope you understood the query, if you have any doubt. Let us know in the comment section.
0 Comments