Corrected Order Pairing
Beginner Mode
Objective
Write a SQL query to correct the swapped order items and return the proper pairing of order ID and item.
Table Schema:
- orders
| Column | Type | Description |
|---|---|---|
| order_id | INTEGER | Sequential order identifier (1, 2, 3, ...) |
| item | VARCHAR | The food item associated with the order |
Task Requirements:
- Adjacent order items have been swapped: order 1 has the item meant for order 2, and vice versa
- Reassign the correct
order_idto each item by swapping adjacent pairs back - Odd
order_idrows should getorder_id + 1, even rows should getorder_id - 1 - If the total number of orders is odd, the last order should remain unchanged
- Sort the results by the corrected order ID in ascending order
Output columns: corrected_order_id, item
Examples
Example 1:
Output:
Input:
| orders | |
|---|---|
| item | order_id |
| Burger | 1 |
| Pizza | 2 |
| Sushi | 3 |
| Pasta | 4 |
| Tacos | 5 |
| Ramen | 6 |
| corrected_order_id | item |
|---|---|
| 1 | Pizza |
| 2 | Burger |
| 3 | Pasta |
| 4 | Sushi |
| 5 | Ramen |
| 6 | Tacos |
Code Environment
Sign in or try as guest to run your code.
Track
| Question | Difficulty | Company | Access |
|---|
Need more practice in this area? Explore more questions →
Instacart