File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed
Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -75,6 +75,7 @@ Have a good contributing!
7575 - [ 184. Department Highest Salary] ( ./leetcode/medium/184.%20Department%20Highest%20Salary.sql )
7676 - [ 570. Managers with at Least 5 Direct Reports] ( ./leetcode/medium/570.%20Managers%20with%20at%20Least%205%20Direct%20Reports.sql )
7777 - [ 602. Friend Requests II: Who Has the Most Friends] ( ./leetcode/medium/602.%20Friend%20Requests%20II:%20Who%20Has%20the%20Most%20Friends.sql )
78+ - [ 626. Exchange Seats] ( ./leetcode/medium/626.%20Exchange%20Seats.sql )
7879 - [ 1045. Customers Who Bought All Products] ( ./leetcode/medium/1045.%20Customers%20Who%20Bought%20All%20Products.sql )
7980 - [ 1070. Product Sales Analysis III] ( ./leetcode/medium/1070.%20Product%20Sales%20Analysis%203.sql )
8081 - [ 1158. Market Analysis 1] ( ./leetcode/medium/1158.%20Market%20Analysis%201.sql )
Original file line number Diff line number Diff line change 1+ /*
2+ Question 626. Exchange Seats
3+ Link: https://leetcode.com/problems/exchange-seats/description/?envType=study-plan-v2&envId=top-sql-50
4+
5+ Table: Seat
6+
7+ +-------------+---------+
8+ | Column Name | Type |
9+ +-------------+---------+
10+ | id | int |
11+ | student | varchar |
12+ +-------------+---------+
13+ id is the primary key (unique value) column for this table.
14+ Each row of this table indicates the name and the ID of a student.
15+ The ID sequence always starts from 1 and increments continuously.
16+
17+
18+ Write a solution 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.
19+
20+ Return the result table ordered by id in ascending order.
21+ */
22+
23+ SELECT
24+ s .id ,
25+ (CASE
26+ WHEN s .id % 2 = 0 THEN s1 .student
27+ WHEN s3 .student IS NULL THEN s .student
28+ WHEN s .id % 2 = 1 THEN s3 .student
29+ END) AS student
30+ FROM Seat AS s
31+ LEFT JOIN
32+ Seat AS s1
33+ ON s .id = (
34+ SELECT MIN (s2 .id ) FROM Seat AS s2
35+ WHERE s1 .id < s2 .id
36+ )
37+ LEFT JOIN
38+ Seat AS s3
39+ ON s .id = (
40+ SELECT MAX (s4 .id ) FROM Seat AS s4
41+ WHERE s3 .id > s4 .id
42+ )
43+
You can’t perform that action at this time.
0 commit comments