SQL Boy
TutorialsPlayground

Format & Validate

SQL FormatterSQL MinifierSyntax Validator

Convert

JSON to SQLCSV to SQLSQL to JSONRegex to SQLExcel to SQLSQL Dialect Convertersoon

Visualize

ER Diagram GeneratorSQL Schema Diff

Generate

SQL Mock Data Generator

Analyze

SQL Query ExplainerSQL Query Analyzer

Workflow hubs

FormattingConversionSchemaAnalysis
View all tools
Daily ChallengeInterviewsCheat SheetBlog

© 2026 SQL Boy. Built for developers.

ContactPrivacyTermsAI Usage PolicyEditorial StandardsAboutRSS Feed

New conversation

What can I help with?

Ask about this page, get a hint on a challenge, or explore SQL concepts.

I know what's on this page and can give answers grounded in SQL Boy content.

Current page

Exchange Seats

/challenges/exchange-seats

Usage status will load after login
SQL Boy
TutorialsPlayground

Format & Validate

SQL FormatterSQL MinifierSyntax Validator

Convert

JSON to SQLCSV to SQLSQL to JSONRegex to SQLExcel to SQLSQL Dialect Convertersoon

Visualize

ER Diagram GeneratorSQL Schema Diff

Generate

SQL Mock Data Generator

Analyze

SQL Query ExplainerSQL Query Analyzer

Workflow hubs

FormattingConversionSchemaAnalysis
View all tools
Daily ChallengeInterviewsCheat SheetBlog
SQL Challenges/Exchange Seats
Challenge DetailMediumReviewed 2026-04-20

Exchange Seats

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.

Case WhenSubqueries

This challenge now meets the current minimum standard for sitemap inclusion and independent indexing.

2025-12-21Medium

Exchange Seats

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.

Available Tables:
SQL Editor
Loading...

What This Challenge Is Testing

This problem tests conditional row remapping, which comes up in scheduling, seat allocation, and sequence normalization tasks. The real skill is preserving the intended final order while transforming row identities in pairs.

Topic Tags

Case WhenAnalytics

Schema At A Glance

seat
idstudent

Solution Approaches

01

CASE expression on odd and even IDs

SELECT
  CASE
    WHEN id % 2 = 1 AND id <> (SELECT COUNT(*) FROM seat) THEN id + 1
    WHEN id % 2 = 1 AND id = (SELECT COUNT(*) FROM seat) THEN id
    ELSE id - 1
  END AS id,
  student
FROM seat
ORDER BY id

This is the standard compact solution. Odd seat IDs normally move forward by one, even IDs move backward by one, and the final odd row stays put when the table has an odd number of seats.

02

Self join neighboring rows conceptually

SELECT
  CASE
    WHEN s1.id % 2 = 1 AND s2.id IS NOT NULL THEN s1.id + 1
    WHEN s1.id % 2 = 1 THEN s1.id
    ELSE s1.id - 1
  END AS id,
  s1.student
FROM seat s1
LEFT JOIN seat s2
  ON s2.id = s1.id + 1
ORDER BY id

The join version makes the pair relationship more visible by checking whether the next seat exists. It is longer, but it can be easier to explain when you want to reason about the odd-last-row case explicitly.

Common Pitfalls

The easiest bug is forgetting to preserve the last row when the number of seats is odd. If you simply add one to every odd ID and subtract one from every even ID, the final odd seat will point to a seat that does not exist.

Frequently Asked Questions

Why do we order by the remapped id at the end?

Because the question asks for the swapped seat arrangement, not the original physical row order. Ordering by the new id produces the final seating sequence the prompt expects.

What happens when the table has an even number of seats?

Then every odd seat has a matching next seat, so all rows swap cleanly in pairs and no special unchanged row is needed.

Concept Guides

Use these guides when you want the fuller explanation behind the SQL pattern this challenge is testing.

SQL CASE Statements: Adding Logic to Your Queries

Learn how to add conditional logic to your SQL queries using CASE statements. Transform data and categorize results with ease.

Understanding SQL Window Functions: A Visual Guide

Master ROW_NUMBER, RANK, and running totals with interactive visualizations. See exactly how window functions process your data row by row.

Mastering SQL Self Joins: A Complete Guide with Interactive Examples

Stop being confused by joins on the same table. Learn how to master Self Joins for organizational hierarchies, sequential data comparison, and more.

Topic Hubs

If this problem exposed a deeper weakness, use a topic hub to move through a curated cluster instead of random article hopping.

Advanced SQL

Advanced query patterns

Use this path when you are moving beyond beginner SELECT queries into CTEs, window functions, and conditional logic.

Practice Path

If you want the rotating practice flow, open this same question from the daily stream. If you want interview-style packaging, continue into the interview pool after this rep.

Back to ChallengesOpen In DailyBrowse Interviews

Related Challenges

Rank ScoresMedium

Assign tied scores the same rank while keeping the next rank consecutive with no gaps.

Tree NodeMedium

Classify each tree node as root, inner, or leaf based on whether it has a parent and whether it is referenced by children.

Rising TemperatureMedium

Compare each weather row with the previous day and return only the IDs where temperature increased day over day.

© 2026 SQL Boy. Built for developers.

ContactPrivacyTermsAI Usage PolicyEditorial StandardsAboutRSS Feed