File tree Expand file tree Collapse file tree 1 file changed +0
-30
lines changed Expand file tree Collapse file tree 1 file changed +0
-30
lines changed Original file line number Diff line number Diff line change 1
- -- 626. Exchange Seats
2
- --
3
- -- Mary is a teacher in a middle school and she has a table seat storing students' names and their corresponding seat ids.
4
- --
5
- -- The column id is continuous increment.
6
- -- Mary wants to change seats for the adjacent students.
7
- -- Can you write a SQL query to output the result for Mary?
8
- -- +---------+---------+
9
- -- | id | student |
10
- -- +---------+---------+
11
- -- | 1 | Abbot |
12
- -- | 2 | Doris |
13
- -- | 3 | Emerson |
14
- -- | 4 | Green |
15
- -- | 5 | Jeames |
16
- -- +---------+---------+
17
- -- For the sample input, the output is:
18
- -- +---------+---------+
19
- -- | id | student |
20
- -- +---------+---------+
21
- -- | 1 | Doris |
22
- -- | 2 | Abbot |
23
- -- | 3 | Green |
24
- -- | 4 | Emerson |
25
- -- | 5 | Jeames |
26
- -- +---------+---------+
27
- -- Note:
28
- -- If the number of students is odd, there is no need to change the last one's seat.
29
-
30
-
31
1
SELECT
32
2
(CASE
33
3
WHEN MOD(id, 2 ) != 0 AND counts != id THEN id + 1
You can’t perform that action at this time.
0 commit comments