Corrected Order Pairing
Instacart ☯️ Medium SQL
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_id to each item by swapping adjacent pairs back
  • Odd order_id rows should get order_id + 1, even rows should get order_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:

Input:

orders
itemorder_id
Burger1
Pizza2
Sushi3
Pasta4
Tacos5
Ramen6

Output:
corrected_order_iditem
1Pizza
2Burger
3Pasta
4Sushi
5Ramen
6Tacos
Quick Solution

Code Environment

Sign in or try as guest to run your code.

Sign In

Track

Question Difficulty Company Access
Need more practice in this area? Explore more questions →