LeetCode 262: Trips and Users [SQL]

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ก.ย. 2024
  • 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 ปีที่แล้ว +1

    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

  • @dmitrychudinovskikh7038
    @dmitrychudinovskikh7038 2 ปีที่แล้ว

    Very elegant! I only managed to solve it with 4 CTEs. But I love CTEs

  • @vn5051
    @vn5051 ปีที่แล้ว

    by far, the best one!! thanks a lot

  • @legion_29
    @legion_29 3 ปีที่แล้ว +1

    Thank you.. Great Explanation and easy to understand!!!

  • @haowang6662
    @haowang6662 3 ปีที่แล้ว

    Wow! Very easy to understand! Huge thanks!!!!

  • @deepshah2137
    @deepshah2137 ปีที่แล้ว

    Excellent explanation!!!!!!!

  • @PATRICKCHUAD
    @PATRICKCHUAD 3 ปีที่แล้ว

    Very good explanation. Than you,

  • @kunalbudhiraja7761
    @kunalbudhiraja7761 2 ปีที่แล้ว

    You made this problem so easy

  • @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 ปีที่แล้ว +1

      you can always use CASE statements instead of IF, just another way of writing it

  • @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. 😮‍💨

  • @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

  • @juancruz8392
    @juancruz8392 3 ปีที่แล้ว

    Great Explanation thank you!!

  • @sabisingh9635
    @sabisingh9635 20 วันที่ผ่านมา

    Why is it a sum and not count in your query? Aren’t we counting number of cancelled rides ??

    • @frederikmuller
      @frederikmuller  19 วันที่ผ่านมา

      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.

  • @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")

  • @adilkevin6220
    @adilkevin6220 2 ปีที่แล้ว

    Could you please continue to make more videos on sql leetcode

  • @vinayp749
    @vinayp749 2 ปีที่แล้ว +1

    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

  • @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'

  • @mohitmanghnani2346
    @mohitmanghnani2346 2 ปีที่แล้ว

    Awesome Bro

  • @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 ปีที่แล้ว +1

      I'll put it on my list of videos to film!

    • @frederikmuller
      @frederikmuller  3 ปีที่แล้ว +4

      it's out now! th-cam.com/video/IFGSqmA2_h8/w-d-xo.html

    • @legion_29
      @legion_29 3 ปีที่แล้ว

      @@frederikmuller thank you very much