Skip to content

Commit 1f1a832

Browse files
Added Exchange Seats
1 parent 22a46e2 commit 1f1a832

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Exchange Seats
3+
https://leetcode.com/problems/exchange-seats/
4+
5+
Mary is a teacher in a middle school and she has a table seat storing students' names and their corresponding seat ids.
6+
7+
The column id is continuous increment.
8+
9+
Mary wants to change seats for the adjacent students.
10+
Can you write a SQL query to output the result for Mary?
11+
12+
+---------+---------+
13+
| id | student |
14+
+---------+---------+
15+
| 1 | Abbot |
16+
| 2 | Doris |
17+
| 3 | Emerson |
18+
| 4 | Green |
19+
| 5 | Jeames |
20+
+---------+---------+
21+
For the sample input, the output is:
22+
23+
24+
+---------+---------+
25+
| id | student |
26+
+---------+---------+
27+
| 1 | Doris |
28+
| 2 | Abbot |
29+
| 3 | Green |
30+
| 4 | Emerson |
31+
| 5 | Jeames |
32+
+---------+---------+
33+
Note:
34+
If the number of students is odd, there is no need to change the last one's seat.
35+
*/
36+
37+
WITH paresAImpares AS (
38+
SELECT (id - 1) AS id, student
39+
FROM seat
40+
WHERE MOD(id, 2) = 0
41+
), imparesAPares AS (
42+
SELECT (id + 1) as id, student
43+
FROM seat
44+
WHERE MOD(id, 2) = 1
45+
)
46+
47+
SELECT id, coalesce(paresAImpares.student, imparesAPares.student, seat.student) AS student
48+
FROM seat LEFT JOIN paresAImpares USING(id) LEFT JOIN imparesAPares using(id)

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ To run a specific problem in your console, go to the file test, add the call to
8181
### Databases
8282
| Problems | Level | Link |
8383
|-|-|-|
84+
| [Department Highest Salary](/LeetcodeProblems/Databases/Department_Highest_Salary.sql) | Medium | https://leetcode.com/problems/department-highest-salary |
85+
| [Exchange Seats](/LeetcodeProblems/Databases/Exchange_Seats.sql) | Medium | https://leetcode.com/problems/exchange-seats |
86+
| [Nth Highest Salary](/LeetcodeProblems/Databases/nth_Highest_Salary.sql) | Medium | https://leetcode.com/problems/nth-highest-salary |
8487
| [Combine Two Tables](/LeetcodeProblems/Databases/Combine_Two_Tables.sql) | Easy | https://leetcode.com/problems/combine-two-tables |
8588
| [Second Highest Salary](/LeetcodeProblems/Databases/Second_highest_salary.sql)| Easy | https://leetcode.com/problems/second-highest-salary |
86-
| [Nth Highest Salary](/LeetcodeProblems/Databases/nth_Highest_Salary.sql) | Medium | https://leetcode.com/problems/nth-highest-salary |
8789
| [Customers Who Never Order](/LeetcodeProblems/Databases/Customers_Who_Never_Order.sql)| Easy | https://leetcode.com/problems/customers-who-never-order |
88-
| [Department Highest Salary](/LeetcodeProblems/Databases/Department_Highest_Salary.sql) | Medium | https://leetcode.com/problems/department-highest-salary/ |
89-
9090

9191
### UtilsClasses
9292

0 commit comments

Comments
 (0)