SQL
  • Tips
  • SQL
    • Database Basics
    • SQL Basics
    • SQL Syntax
    • Retrieve Data: SELECT
    • Sort Data: ORDER BY
    • Filter Data: WHERE
    • Calculated Fields
    • Aggregate Functions
    • Group Data: GROUP BY
    • Subqueries
    • Join Tables
    • Combine Queries: UNION
    • Control Flow Statements
    • IF function
    • Handle NULL
    • Date
    • Numeric
    • String
    • Notes
  • Table/Database
    • Insert
    • Delete
    • Update
    • Table
    • Database
    • Stored Procedure
  • Misc
    • SQL vs NoSQL
    • 大数据
    • Why SQL instead of Excel + VBA?
  • sqlzoo
    • world table
    • nobel table
    • football data
    • movie data
    • Teacher Department Data
    • Edinburgh Buses data
  • Leetcode
    • 175_Combine Two Tables
    • 176_Second Highest Salary
    • 177_Nth Highest Salary
    • 178_Rank Scores
    • 180_Consecutive Numbers
    • 181_Employees Earning More Than Their Managers
    • 182_Duplicate Emails
    • 183_Customers Who Never Order
    • 184_Department Highest Salary
    • 185_Department Top Three Salaries
    • 196_Delete Duplicate Emails
    • 197_Rising Temperature
    • 570_Managers with at Least 5 Direct Reports
    • 578_Get Highest Answer Rate Question
    • 579_Find Cumulative Salary of an Employee
    • 584_Find Customer Referee
    • 586_Customer Placing the Largest Number of Orders
    • 595_Big Countries
    • 596_Classes More Than 5 Students
    • 597_Friend Requests I: Overall Acceptance Rate
    • 601_Human Traffic of Stadium
    • 602_Friend Requests II: Who Has the Most Friends
    • 603_Consecutive Available Seats
    • 607_Sales Person
    • 608_Tree Node
    • 610_Triangle Judgement
    • 612_Shortest Distance in a Plane
    • 613_Shortest Distance in a Line
    • 619_Biggest Single Number
    • 620_Not Boring Movies
    • 626_Exchange Seats
    • 627_Swap Salary
  • Facebook 面经题
    • spam filter
    • marketplace
    • instagram
    • session
    • message confirmation
Powered by GitBook
On this page
  • Solution 1: change name
  • Solution 2: change id
  • Solution 3: using two UNION operators

Was this helpful?

  1. Leetcode

626_Exchange Seats

Mary is a teacher in a middle school and she has a tableseatstoring students' names and their corresponding seat ids.

The column id is continuous increment.

Mary wants to change seats for the adjacent students.

Can you write a SQL query to output the result for Mary?

+---------+---------+
|    id   | student |
+---------+---------+
|    1    | Abbot   |
|    2    | Doris   |
|    3    | Emerson |
|    4    | Green   |
|    5    | Jeames  |
+---------+---------+

For the sample input, the output is:

+---------+---------+
|    id   | student |
+---------+---------+
|    1    | Doris   |
|    2    | Abbot   |
|    3    | Green   |
|    4    | Emerson |
|    5    | Jeames  |
+---------+---------+

Note: If the number of students is odd, there is no need to change the last one's seat.

Solution 1: change name

  • Left join seat table twice to get the previous name and the next name for each name

  • Use CASE WHEN to differentiate if we want to use the previous name or the next name

  • Use MOD(id, 2) = 0 or 1 to differentiate if the id is odd or even

  • Handle the last odd case if it exist with IFNULL() function

# Write your MySQL query statement below
SELECT s1.id,
    (
    CASE
        WHEN MOD(s1.id, 2) = 0 THEN s3.student
        WHEN MOD(s1.id, 2) = 1 THEN IFNULL(s2.student, s1.student)
    END
    ) AS student
FROM seat AS s1
    LEFT JOIN seat AS s2 ON s2.id - s1.id = 1
    LEFT JOIN seat AS s3 ON s1.id - s3.id = 1
ORDER BY s1.id ASC;

Solution 2: change id

For students with odd id, the new id is (id+1) after switch unless it is the last seat. And for students with even id, the new id is (id-1).

SELECT
    (CASE
        WHEN MOD(id, 2) != 0 AND counts != id THEN id + 1
        WHEN MOD(id, 2) != 0 AND counts = id THEN id
        ELSE id - 1
    END) AS id,
    student
FROM
    seat,
    (SELECT
        COUNT(*) AS counts
    FROM
        seat) AS seat_counts
ORDER BY id ASC;

Solution 3: using two UNION operators

/* get all the even numbered rows as odd numbered rows */
SELECT s1.id - 1 as id, s1.student
FROM Seat s1
WHERE MOD(s1.id, 2) = 0

UNION

/* get all the odd numbered rows as even numbered rows */
SELECT s2.id + 1 as id, s2.student
FROM Seat s2
WHERE MOD(s2.id, 2) = 1 AND s2.id != (SELECT MAX(id) FROM Seat)
/* Just don't get the last row as we will handle it in the next UNION */

UNION

/* get the last row if odd and don't change the id value */
SELECT s3.id, s3.student
FROM Seat s3
WHERE MOD(s3.id, 2) = 1 AND s3.id = (SELECT MAX(id) FROM Seat)

/* Order the result by id */
ORDER BY id ASC;
Previous620_Not Boring MoviesNext627_Swap Salary

Last updated 5 years ago

Was this helpful?