LeetCode 197: Rising Temperature [SQL]

  Рет қаралды 18,892

Frederik Müller

Frederik Müller

Күн бұрын

Solution and walkthrough of leetcode database problem 197: Rising Temperature. I'm using MySQL but this solution should work in any SQL dialect such as PostgreSQL SQL Server, etc.
Link to the problem: leetcode.com/p...

Пікірлер: 33
@rskmahesh3531
@rskmahesh3531 8 ай бұрын
Dude this is the best explanation for this question on entire youtube.
@ft_peakhd2921
@ft_peakhd2921 Жыл бұрын
Hi Frederik, Out of no where your channel popped up in my feed. Now, i can't stop watching your videos. Your explanation is very understandable and your approach to every problem is very good. You deserve a subscribe. Thank you, keep making more videos.
@lakshmanank1950
@lakshmanank1950 Жыл бұрын
Hi Frederik, your different approach to problem-solving is very useful. thanks.
@summer_in_california
@summer_in_california Жыл бұрын
THIS IS THE BEST SOLUTION EVER. VERY SIMPLE AND CLEAR! THANK YOU SOOOO MUCHHHHH
@alibhaiacademy2981
@alibhaiacademy2981 3 жыл бұрын
My solution but still 33.73% WITH temp AS ( SELECT id, temperature, recorddate, LAG(temperature, 1) OVER(ORDER BY recorddate) AS previous_temperature, LAG(recorddate, 1) OVER(ORDER BY recorddate) AS previous_date FROM weather ) SELECT id FROM temp WHERE DATEDIFF(recorddate, previous_date) = 1 AND temperature > previous_temperature
@rohitekka2674
@rohitekka2674 3 жыл бұрын
Thanks alot for enlightening us with different approaches. Great explanation and video as always
@RahmatKhanZai89
@RahmatKhanZai89 Жыл бұрын
حیرانوونکی کار. دا زما سره د ستونزې په پوهیدو کې مرسته وکړه. همدارنګه دا یو ډیر ساده حل دی.
@frederikmuller
@frederikmuller Жыл бұрын
ستاسو د نظر څخه مننه
@spyboy0076
@spyboy0076 8 ай бұрын
u got a sub man. thanks
@inzamam483
@inzamam483 2 жыл бұрын
WHY THIS SOLUTION is giving an error SELECT id FROM (SELECT *,LEAD(temperature,1,0 ) OVER(ORDER BY id ) as tem FROM Weather) AS wea WHERE tem < temperature
@inzamam483
@inzamam483 2 жыл бұрын
WHY THIS SOLUTION is giving an error SELECT id FROM (SELECT id, recordDate, temperature, LEAD(temperature,1,0 ) OVER(ORDER BY id ) as tem, LAG(RECORDDATE,1,0) OVER (ORDER BY RECORDDATE) AS D1, RECORDDATE AS D2 FROM Weather) AS wea WHERE tem < temperature AND TO_DAYS(wea.D2)-TO_DAYS(wea.D1)=1
@abhinavkumar6584
@abhinavkumar6584 2 жыл бұрын
why didn't we do it via id difference?
@karthikbs8457
@karthikbs8457 2 жыл бұрын
In MS SQL the DATEDIFF subtracts like seconddate--firstdate. In Mysql its the opposite
@frederikmuller
@frederikmuller 2 жыл бұрын
interesting 🤔
@karthikbs8457
@karthikbs8457 2 жыл бұрын
@@frederikmuller Fredrick what are all the concepts in SQL which are termed as Advanced SQL?
@frederikmuller
@frederikmuller 2 жыл бұрын
@@karthikbs8457 I'd say window functions, CTEs, HAVING, complex OUTER JOINs, user defined variables
@SchreiEs
@SchreiEs 2 жыл бұрын
SQL Server solutions: --Method 1 with cte1 as ( select * ,lag(temperature,1) over (order by recordDate) as previous_temperature from weather ) select id from cte1 where temperature > previous_temperature; --Method 2 select curr.id from weather curr join weather prev on curr.id = prev.id + 1 where curr.temperature > prev.temperature; --Method 3 select curr.id from weather curr join weather prev on datediff(day,curr.recordDate,prev.recordDate) = -1 where curr.temperature > prev.temperature;
@mickyman753
@mickyman753 2 жыл бұрын
select p1.id from Weather p1,Weather p2 where p1.temperature>p2.temperature and DateDiff(p1.recordDate,p2.recordDate)=1;
@shrutisugandhi1172
@shrutisugandhi1172 2 жыл бұрын
sheer genius!
@viveksivalingam9181
@viveksivalingam9181 4 жыл бұрын
Hi Fredrik What if the dates are not consecutive ? LeetCode uses dates that are not consecutive as a test case when you hit 'Submit' tx
@frederikmuller
@frederikmuller 4 жыл бұрын
Hi Vivek, the question specifically states: Write an SQL query to find all dates' id with higher temperature compared to its previous dates (yesterday). However, if you were to allow for gaps in the data, you could use window functions and lag/lead functions to find the row above/below the current date and compare to it. You could also join on w2.recordDate > w1.recordDate and then select MIN(w2.recordDate) to get the smallest date that is bigger than the one you compare to (make sure you also group by w1.recordDate then). Hope this explanation wasn't too confusing.
@ketanverma7839
@ketanverma7839 Жыл бұрын
can lead or lag be used in this type of question ?
@frederikmuller
@frederikmuller Жыл бұрын
yes, absolutely, you could compare temperature to the LAG of temperature to solve this question
@AbhishekSharma-hy4nl
@AbhishekSharma-hy4nl 3 жыл бұрын
But how to do this on sql server. DATEDIFF is giving error in sql server.
@frederikmuller
@frederikmuller 3 жыл бұрын
You have to specify an interval in the function in SQL Server, it would be DATEDIFF (day, w1.recordDate, w2.recordDate) then. In MySQL it's in days by default.
@AbhishekSharma-hy4nl
@AbhishekSharma-hy4nl 3 жыл бұрын
@@frederikmuller thnx it worked.👍
@frederikmuller
@frederikmuller 3 жыл бұрын
Great! There are slight differences between SQL dialects but usually everything is available everywhere. Functions working slightly differently here is a good example.
@venkatmanchikalapudi6448
@venkatmanchikalapudi6448 2 жыл бұрын
select w1.id from weather w1 join weather w2 on w1.id+1 = w2.id and w1.temperature > w2.temperature;. hi why does this doesn't work?
@ishitvasingh9902
@ishitvasingh9902 2 жыл бұрын
you have to write w2.temperature > w1.temperature and select w2.id that is why you have to go step by step firstly when you join see the result then apply conditions
@leasunsun
@leasunsun 2 жыл бұрын
select w2.id from weather w1 join weather w2 on w1.id+1 = w2.id and w1.temperature < w2.temperature;
@mickyman753
@mickyman753 2 жыл бұрын
as the id don't have somthing to do with dates , you can have a id=3 of tuesday , and id=2 for wednesday , we strictly have to previous day , which can only be identified by dates
@maheshodedra8609
@maheshodedra8609 3 жыл бұрын
Hi Frederik, Thanks for the great explanation, would you please look into my solution and let me know if any issues? select w2.id from weather w1 join weather w2 on w1.id+1 = w2.id and w2.temprature > w1.temprature Looking forward to your response. :-) Thanks Mahesh
LeetCode 1661: Average Time of Process per Machine [SQL]
8:16
Frederik Müller
Рет қаралды 9 М.
How To Get Married:   #short
00:22
Jin and Hattie
Рет қаралды 26 МЛН
Will A Guitar Boat Hold My Weight?
00:20
MrBeast
Рет қаралды 270 МЛН
UUID vs INT: What’s Better For Your Primary Key?
9:40
Database Star
Рет қаралды 46 М.
LeetCode 1581: Customer Who Visited but Did Not Make Any Transactions [SQL]
11:52
LeetCode 262: Trips and Users [SQL]
9:28
Frederik Müller
Рет қаралды 9 М.
What does a Data Analyst actually do? (in 2024) Q&A
14:27
Tim Joo
Рет қаралды 64 М.
LeetCode 1204: Last Person to Fit in the Elevator/Bus [SQL]
10:26
Frederik Müller
Рет қаралды 4,8 М.
LeetCode 1251 Interview SQL Question with Detailed Explanation | Practice SQL
17:11
Three Tricky Analytics Interview Questions with Andrew
25:03
Jay Feng
Рет қаралды 81 М.
197. Rising Temperature - LeetCode SQL Solution [EASY]
6:28
Code with Carter
Рет қаралды 952
How To Get Married:   #short
00:22
Jin and Hattie
Рет қаралды 26 МЛН