Skip to content
Onsite

Swap adjacent seat ids

MediumP3Coding

Table seat:

ColumnType
idint (primary key, 1, 2, 3, ... with no gaps)
studenttext

Swap the students of every two adjacent seats: seat 1 with seat 2, seat 3 with seat 4, and so on. If the number of seats is odd, the last student keeps their seat. Ids stay in place, only names move.

Output columns: id, student. Order by id.

Schema

CREATE TABLE seat (
  id int PRIMARY KEY,
  student text NOT NULL
);

Example 1

Input
seat: (1, A), (2, B), (3, C), (4, D)
Output
(1, B), (2, A), (3, D), (4, C)

Example 2

Input
seat: (1, A), (2, B), (3, C)
Output
(1, B), (2, A), (3, C)

Constraints

  • PostgreSQL dialect.

Hints

PostgreSQL
Run checks the 3 visible tests, Submit all 6.