SQL
All Top 50 SQL Interview Questions#48
Swap Consecutive Seat IDs in SQL
MediumGoogleInterview Question #48
Asked at GoogleWrite a SQL query to swap the `id` of every two consecutive students in a `seat` table. If the number of students is odd, the id of the last student is not swapped.
Input Table: seat
5 rows preview| id | student |
|---|---|
| 1 | Abbot |
| 2 | Doris |
| 3 | Emerson |
| 4 | Green |
| 5 | Jeames |
Expected Output Structure5 rows
| id | student |
|---|---|
| 1 | Doris |
| 2 | Abbot |
| 3 | Green |
| 4 | Emerson |
| 5 | Jeames |
Interview Context
Asked frequently in data analyst and business analyst technical rounds. Focus on clean filtering, optimal indexing usage, and unambiguous column selection.
PostgreSQL 15
Ready to execute
Click "Run Query" or press Ctrl + Enter to test your query.
Output will be verified against the test dataset.