LeetCode Medium 626 Amazon Interview SQL Question with Detailed Explanation

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ม.ค. 2025

ความคิดเห็น •

  • @dantenekomew
    @dantenekomew 9 หลายเดือนก่อน +1

    Best approach, getting previous and next is easier to understand than, let's say id+1 and id-1

    • @EverydayDataScience
      @EverydayDataScience  9 หลายเดือนก่อน

      Glad that you found the video useful 😊

  • @adityams1659
    @adityams1659 2 ปีที่แล้ว +4

    *I figured out a more efficient and elgegant solution to this*
    SELECT
    CASE WHEN (select max(id) from Seat)%2 = 1 and id = (SELECT max(id) FROM Seat) THEN id
    WHEN id%2=1 THEN id+1
    WHEN id%2=0 THEN id-1 END AS id,
    student
    FROM Seat
    ORDER BY id

  • @raghavendrac4710
    @raghavendrac4710 6 หลายเดือนก่อน

    Your videos are very helpful. Thanks

  • @bhanukoushikthyarla2682
    @bhanukoushikthyarla2682 6 หลายเดือนก่อน

    Good Explanation Sir !

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

    The approach is really good 👍

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

    hi !! Great video . I just want to ask how to develop logic building skills for SQL Queries

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

    Nice approach and well explained. Thank you!

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

    with cte as (
    select student,
    case when id%2 = 1 then id+1 else id-1 end as new_id
    from seat
    order by new_id)
    select ROW_NUMBER() over()id ,student from cte

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

    with cte1 as(
    select case when id = (select max(id) from Seat) then id
    when id%2 = 0 then id-1
    when id%2 = 1 then id+1 else 'null' end as id , student
    from Seat
    where (select max(id) from Seat)%2 = 1
    order by id asc) , cte2 as
    (
    select case when id%2 = 0 then id-1
    when id%2 = 1 then id+1 else 'null' end as id , student
    from Seat
    where (select max(id) from Seat)%2 = 0
    order by id asc )
    select * from cte1
    union all
    select * from cte2

  • @muddassirnazar9275
    @muddassirnazar9275 2 หลายเดือนก่อน

    Simple way:/* Write your T-SQL query statement below */
    with cte as
    (select *,
    lag(student) over(order by id) as prev,
    lead(student) over(order by id) as nextt
    from seat)
    select id,case when id%2=0 then prev
    when id%2!=0 and nextt is not null then nextt
    else student end as student
    from cte

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

    with cte as (
    select id, student as original_student, case when id % 2 = 1 then lead(student,1) over(order by id) else
    lag(student,1) over(order by id) end as student
    from seat)
    select id, case when student is null then original_student else student end
    as student
    from cte

  • @sahilnagar1767
    @sahilnagar1767 11 หลายเดือนก่อน +1

    How about this?
    SELECT
    id,
    CASE
    WHEN id % 2 = 1 THEN LEAD(student,1, student) OVER (ORDER BY id)
    WHEN id % 2 = 0 THEN LAG(student) OVER (ORDER BY id)
    END AS student
    FROM
    Seat;

    • @mickyman753
      @mickyman753 26 วันที่ผ่านมา

      how are you handling the last column case where the id can be odd?

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

    select a.id , CASE WHEN B.STUDENT IS NULL THEN a.STUDENT
    WHEN a.id%2 =0 THEN c.STUDENT else b.student end as student
    From Seat a left join Seat b
    on a.id+1 = b.id
    left join Seat c
    on a.id = c.id + 1

  • @mickyman753
    @mickyman753 26 วันที่ผ่านมา

    with cte1 as (
    select id,student,lead(id) over(order by id) as next,
    lag(id) over(order by id) as prev
    from seat),
    cte2 AS
    ( SELECT id, student,
    CASE
    WHEN id = (SELECT COUNT(*) FROM seat) THEN
    CASE
    WHEN id % 2 != 0 THEN id
    ELSE prev
    END
    ELSE
    CASE
    WHEN id % 2 = 0 THEN prev
    ELSE next
    END
    END AS swap_id FROM cte1 )
    select swap_id as id,student from cte2 order by id;

  • @pratikjugantmohapatra18
    @pratikjugantmohapatra18 3 หลายเดือนก่อน

    WITH cte AS (
    SELECT id, student,
    LAG(student) OVER (ORDER BY id) AS prev_name,
    LEAD(student) OVER (ORDER BY id) AS next_name
    FROM Seat
    )
    SELECT id,
    CASE
    WHEN id % 2 = 1 AND next_name IS NOT NULL THEN next_name
    WHEN id % 2 = 0 THEN prev_name
    ELSE student
    END AS student
    FROM cte
    ORDER BY id;