LEETCODE - Passenger Bus Problem - SQL Interview Query 23| SQL Problem Level "HARD"

Поділитися
Вставка
  • Опубліковано 4 жов 2024

КОМЕНТАРІ • 23

  • @Szwagier-fr7ry
    @Szwagier-fr7ry 6 місяців тому +4

    :O Brain blow, thanks to this video I saw for the first time in my life Recursive function in SQL. Love it - thanks!

    • @theLowestPointInMyLife
      @theLowestPointInMyLife 6 місяців тому +1

      reason for that is you would never need it irl, this is just a stupid made up problem

    • @SanjayKumar-sr5ft
      @SanjayKumar-sr5ft 5 місяців тому

      @@theLowestPointInMyLife yes this may be a madeup question,but this certainly improves how to approach "reccursive" question...i mean how to relate pervious iteration and current iteration,which may be a real scenario

  • @vikaskumar-qr5tj
    @vikaskumar-qr5tj 6 місяців тому

    Nice way of approaching the problem and one very good use case of recursive cte..

  • @pralaysangma8019
    @pralaysangma8019 6 місяців тому

    I learnt many things from your videos on SQL Server. Thank you Taufiq.

  • @ajinkyatrimukhe
    @ajinkyatrimukhe 6 місяців тому +1

    Mind blown

  • @Rameshkumar-dk8me
    @Rameshkumar-dk8me 6 місяців тому

    Thanks Thoufig with nice problem, please explain the subtraction logic in detail[logic part] .

  • @balukrishna2581
    @balukrishna2581 6 місяців тому

    Please explain a queries related to mutual funds and stocks in this 30 days challenge.

  • @Tusharchitrakar
    @Tusharchitrakar 6 місяців тому +2

    Thanks for finally uploading this leetcode problem based on our discussion a week back. I have also posted my MySQL solution for others. I have one request for a future video: since you focussed so much on improving the speed of the query, can you make a future video on query optimization techniques and tips? It'll be very very helpful especially from a work point of view where we would be expected to write 200-1000 lines of SQL code. Thanks!
    -- to join passengers to all possible buses and calculate total passengers possible for each bus
    -- View is created since a recursive cte cannot access a regular cte in mysql
    create view max_passengers as
    (
    select bus.bus_id, bus.capacity, count(*) as max_poss
    from buses as bus join passengers as pass
    on pass.arrival_time

  • @akashchristopher
    @akashchristopher 6 місяців тому +1

    should i expect these kind of problems from top tech companie's interviews ?

  • @tanujreddy8597
    @tanujreddy8597 Місяць тому

    should i expect these kind of problems from top tech companie's interviews for freshers?

  • @muhammadabbas6645
    @muhammadabbas6645 6 місяців тому +1

    Hello Sir hope you are doing well sorry to disturb you again as after watching this video more than 2 times I an unable to understand why we need total_onboarded_pessangers can you please elaborate as my answer is quite similar with yours but I didnt add total_onboarded_pessangers and got my answer right thanks you for all your videos as it makes me good in sql language God bless you and your family

    • @techTFQ
      @techTFQ  6 місяців тому +2

      You need to test your solution with other datasets. I have shared 5 different sample data .. check if your solution works for them .. you will probably then realize why total onboarding field is required

  • @Ri9t_gaming
    @Ri9t_gaming 5 місяців тому +1

    Is there a way to check which passengers onboarded which bus? or can there be another case where single bus arrives multiple times.

  • @eniolaabolade8638
    @eniolaabolade8638 6 місяців тому

    Hi, can you please post more EDA projects using Python. I love your content 🥺

  • @firojahmed785
    @firojahmed785 3 місяці тому

    Hello,
    I tried to run your MSSQL query, but it shows no rows.
    Is there something I am missing?

  • @fathimafarahna2633
    @fathimafarahna2633 6 місяців тому

    🔥🔥🔥

  • @MaciejGozdur
    @MaciejGozdur 4 місяці тому

    18.49 how did you get the column total_onboarded_bus? you named two columns onboarded_bus yet they are displayed with different names

    • @shubhamsom
      @shubhamsom 3 місяці тому

      columns names displayed are based on what you have written at initialization state of recursion cte , since column-name was onboarded_bus at rn=1, now you might know the reason

  • @tanmaykumar3250
    @tanmaykumar3250 12 днів тому

    this one is without recursive cte using window function. Though using recursive cte looks easier and good approach. But here is my solution :
    WITH bus AS (
    SELECT
    bus_id,
    arrival_time,
    IFNULL(LAG(arrival_time) OVER(ORDER BY arrival_time ASC), 0) AS prev_bus_time,
    capacity
    FROM buses
    ),
    cte AS (
    SELECT
    b.bus_id,
    b.arrival_time AS bus_arrival,
    b.prev_bus_time,
    b.capacity,
    p.*
    FROM bus AS b
    LEFT JOIN Passengers AS p
    ON p.arrival_time b.prev_bus_time
    ORDER BY b.arrival_time
    ),
    cte2 AS (
    SELECT
    bus_id,
    ROUND(AVG(bus_arrival)) AS bus_arrival,
    ROUND(AVG(capacity)) AS capacity,
    COUNT(DISTINCT passenger_id) AS passengers
    FROM cte
    GROUP BY bus_id
    ORDER BY ROUND(AVG(bus_arrival))
    ),
    cte3 AS (
    SELECT
    *,
    IF(
    passengers - capacity + IF(
    IFNULL(LAG(passengers) OVER(ORDER BY bus_arrival), 0) -
    IFNULL(LAG(capacity) OVER(ORDER BY bus_arrival), 0) > 0,
    IFNULL(LAG(passengers) OVER(ORDER BY bus_arrival), 0) -
    IFNULL(LAG(capacity) OVER(ORDER BY bus_arrival), 0),
    0
    ) > 0,
    passengers - capacity + IF(
    IFNULL(LAG(passengers) OVER(ORDER BY bus_arrival), 0) -
    IFNULL(LAG(capacity) OVER(ORDER BY bus_arrival), 0) > 0,
    IFNULL(LAG(passengers) OVER(ORDER BY bus_arrival), 0) -
    IFNULL(LAG(capacity) OVER(ORDER BY bus_arrival), 0),
    0
    ),
    0
    ) AS passengers_on_platform
    FROM cte2
    )
    SELECT
    bus_id,
    CASE
    WHEN capacity >= passengers + IFNULL(LAG(passengers_on_platform) OVER(ORDER BY bus_arrival), 0)
    THEN passengers + IFNULL(LAG(passengers_on_platform) OVER(ORDER BY bus_arrival), 0)
    ELSE capacity
    END AS passengers_in_bus
    FROM cte3
    ORDER BY bus_id;