LeetCode 262: Trips and Users [SQL]

  Рет қаралды 9,140

Frederik Müller

Frederik Müller

Күн бұрын

Solution and walkthrough of leetcode database problem 262: Trips and Users. 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...

Пікірлер: 31
@FLYAEROBOY
@FLYAEROBOY 2 жыл бұрын
Hands down you're amazing when it comes to explaining and solving these queries - You explain just about enough - no blabber - straight and concise. Please keep continuing the good work. Subscribed, thank you!
@zhihaoxu8119
@zhihaoxu8119 3 жыл бұрын
Thanks! Your approach is more intuitive than using inner join twice
@anirbansarkar6306
@anirbansarkar6306 2 жыл бұрын
Awesome explanation. You divided this complex problem into such simple solutions and added them one after another, which just cleared the concepts in best way possible. Thanks a lot @Frederik Müller
@vn5051
@vn5051 Жыл бұрын
by far, the best one!! thanks a lot
@legion_29
@legion_29 3 жыл бұрын
Thank you.. Great Explanation and easy to understand!!!
@haowang6662
@haowang6662 3 жыл бұрын
Wow! Very easy to understand! Huge thanks!!!!
@akhandbharat2463
@akhandbharat2463 3 жыл бұрын
you have a great approach to complex problems. That's what makes it easier to understand. Do you think we could have used CASE statements too?
@frederikmuller
@frederikmuller 3 жыл бұрын
you can always use CASE statements instead of IF, just another way of writing it
@dmitrychudinovskikh7038
@dmitrychudinovskikh7038 2 жыл бұрын
Very elegant! I only managed to solve it with 4 CTEs. But I love CTEs
@frederikmuller
@frederikmuller 2 жыл бұрын
you do you Dmitry!
@deepshah2137
@deepshah2137 Жыл бұрын
Excellent explanation!!!!!!!
@evanpetrarca
@evanpetrarca 9 ай бұрын
My solution with CTEs is much more involved and only passes 9 of 12 test cases. I did try your solution but can't seem to get my values to become a float like yours. I tried casting and multiplying by 1.0 but no luck. 😮‍💨
@kunalbudhiraja7761
@kunalbudhiraja7761 2 жыл бұрын
You made this problem so easy
@PATRICKCHUAD
@PATRICKCHUAD 3 жыл бұрын
Very good explanation. Than you,
@vinayp749
@vinayp749 2 жыл бұрын
Hello Frederik I have tried the solution and it's accepting the answer btu throwing an error in submission as my calculation is not rounding to 1.00. can you please tell me where I am missing the logic (select request_at, count(case when status in ('cancelled_by_driver','cancelled_by_client') then 1 else Null end) as no_of_cancelled,count(1) as no_of_trips, round(1.0*count(case when status in ('cancelled_by_driver','cancelled_by_client') then 1 else Null end)/count(1),2) as percent from trips t inner join users c on t.client_id=c.users_id inner join users d on t.driver_id=d.users_id where c.banned='No' and d.banned='No' group by request_at)
@srikanthyellapu5709
@srikanthyellapu5709 Жыл бұрын
try select t.Request_at Day, ROUND( (cast(sum(IIF(t.status!='completed',1,0)) as float) / count(*)) ,2) as 'Cancellation Rate' from Trips t where t.Client_Id in (Select Users_Id from Users where Banned='No') and t.Driver_Id in (Select Users_Id from Users where Banned='No') and t.Request_at between '2013-10-01' and '2013-10-03' group by t.Request_at
@juancruz8392
@juancruz8392 3 жыл бұрын
Great Explanation thank you!!
@saurabhanand9110
@saurabhanand9110 Жыл бұрын
Hi there can someone help- I'm getting wrong values for cancellation rate : with tt as (select status, request_at from Trips a Join Users b ON a.client_id = b.users_id OR a.driver_id = b.users_id WHERE banned = 'NO' AND request_at BETWEEN "2013-10-01" AND "2013-10-03") select request_at as "Day", ROUND(SUM(IF(status != "completed",1,0))/count(status),2) as "Cancellation Rate" from tt group by Day
@ankitatapadia4806
@ankitatapadia4806 3 жыл бұрын
For the banned users check, if I wrote the code in the below mentioned way and it is accepted on LeetCode. But my question is will it make any difference on any other test case? where client_id IN (select users_id from users where banned ="No") and driver_id IN (select users_id from users where banned ="No")
@sabisingh9635
@sabisingh9635 26 күн бұрын
Why is it a sum and not count in your query? Aren’t we counting number of cancelled rides ??
@frederikmuller
@frederikmuller 24 күн бұрын
a SUM still works here as the IF statement within the SUM function gives 1 for cancelled rides, that way we’re summing up or counting the cancelled rides. you could also use COUNT if you restructure the IF statement to assign NULL to non-cancelled rides.
@adilkevin6220
@adilkevin6220 2 жыл бұрын
Could you please continue to make more videos on sql leetcode
@mohitmanghnani2346
@mohitmanghnani2346 2 жыл бұрын
Awesome Bro
@frederikmuller
@frederikmuller 2 жыл бұрын
Awesome comment bro
@nikhilasati2329
@nikhilasati2329 Жыл бұрын
Not sure what's wrong in the following: With CTE1 AS ( Select Users.users_id, Trips.request_at, case when Trips.status like ('%cancelled%') then 1 else 0 end as status_flag FROM Users JOIN Trips ON Users.Users_id = Trips.client_id where Users.banned in ('No') and Users.role = ('client') ) , CTE2 AS ( Select Users.users_id, Trips.request_at, case when Trips.status like ('%cancelled%') then 1 else 0 end as status_flag FROM Users JOIN Trips ON Users.Users_id = Trips.driver_id where Users.banned in ('No') and Users.role = ('client') ) , CTE3 AS ( Select * from CTE1 Union all Select * from CTE2 ) , CTE4 AS ( select request_at, count(*) as cnt, sum(status_flag) as sm from CTE3 group by request_at ) select request_at as Day, round((sm/cnt),2) as "Cancellation Rate" from CTE4 where request_at between '2013-10-01' and '2013-10-03'
@karthikbs8749
@karthikbs8749 2 жыл бұрын
I solved this question on my own but took two hours and the code was very lengthy. May be I need some optimising and better thinking skills.
@frederikmuller
@frederikmuller 2 жыл бұрын
hm maybe try some more medium or even easy questions as this question is like a combination of multiple questions (calculate a ratio, use NOT IN, JOIN tables…). if you get good at all these elements and recognize what the question is asking for you should be able to solve these hard ones rather quickly!
@legion_29
@legion_29 3 жыл бұрын
Could you pls make a video on leetcode 1270. All People Report to the Given Manager. Thank you
@frederikmuller
@frederikmuller 3 жыл бұрын
I'll put it on my list of videos to film!
@frederikmuller
@frederikmuller 3 жыл бұрын
it's out now! kzbin.info/www/bejne/f3eqhKSjdpeSnpo
@legion_29
@legion_29 3 жыл бұрын
@@frederikmuller thank you very much
LeetCode 511: Game Play Analysis I [SQL]
3:15
Frederik Müller
Рет қаралды 7 М.
LeetCode 1454: Active Users [SQL]
14:01
Frederik Müller
Рет қаралды 6 М.
Running With Bigger And Bigger Feastables
00:17
MrBeast
Рет қаралды 211 МЛН
나랑 아빠가 아이스크림 먹을 때
00:15
진영민yeongmin
Рет қаралды 16 МЛН
I Took a LUNCHBAR OFF A Poster 🤯 #shorts
00:17
Wian
Рет қаралды 16 МЛН
Brawl Stars Edit😈📕
00:15
Kan Andrey
Рет қаралды 10 МЛН
Leetcode Hard Problem 4 | User Purchase Platform | Complex SQL 11
12:51
LeetCode 601: Human Traffic of Stadium [SQL]
10:57
Frederik Müller
Рет қаралды 7 М.
What does a Data Analyst actually do? (in 2024) Q&A
14:27
Tim Joo
Рет қаралды 56 М.
262. Trips and Users | Leetcode SQL Hard
14:02
Tech Timeout
Рет қаралды 500
I Solved 1583 Leetcode Questions  Here's What I Learned
20:37
ThePrimeTime
Рет қаралды 634 М.
Google SQL Interview Problem | Solving SQL Interview Query
14:22
LeetCode 185: Department Top Three Salaries [SQL]
9:33
Frederik Müller
Рет қаралды 14 М.
Running With Bigger And Bigger Feastables
00:17
MrBeast
Рет қаралды 211 МЛН