SQL 문제풀이
leecode - 626. Exchange Seats
Jerrytwo
2022. 7. 8. 16:59
난이도 : Medium
Table: Seat
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| name | varchar |
+-------------+---------+
id is the primary key column for this table.
Each row of this table indicates the name and the ID of a student.
id is a continuous increment.
Write an SQL query to swap the seat id of every two consecutive students. If the number of students is odd, the id of the last student is not swapped.
Return the result table ordered by id in ascending order.
SELECT CASE WHEN MOD(id, 2) = 1 AND id = (SELECT MAX(id) FROM Seat) THEN id
WHEN MOD(id, 2) = 0 THEN id - 1
ELSE id + 1
END id
, student
FROM Seat
ORDER BY id
Accepted (9.11%)
SELECT ROW_NUMBER() OVER () id
, student
FROM Seat
ORDER BY IF(MOD(id, 2) = 0, id - 1, id + 1)
Accepted (68.17%)