Very simple solution using simple SELECT statement, no joins


  • 0
    M
    SELECT S1.id, S2.student 
    FROM seat as S1, seat as S2
    WHERE ((S1.id % 2 = 0) AND (S1.id - S2.id = 1))
    OR  ((S1.id % 2 = 1) AND (S2.id - S1.id = 1))
    OR S1.id = (SELECT MAX(id) FROM seat) AND S2.id = (SELECT MAX(id) FROM seat) AND S1.id %2 = 1
    ORDER BY S1.id
    

    as you can see using a clone of the table, you can swap Odd students with even students then put a condition to handle the case of having last record as odd record.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.