Premium problem36. Repair Swapped Delivery Labels

Medium Locked

deliveries(delivery_id, customer_name, delivered_at)

Names were swapped between adjacent delivery pairs: 1 with 2, 3 with 4, and so on. Undo it.

Return delivery_id and corrected_customer_name, ordered by delivery_id. If the highest id has no partner, it keeps its own name.

An odd id takes the name of the id above it, an even id takes the name of the id below, and the unpaired last row falls back to itself.

Tables

deliveries

delivery_id  customer_name  delivered_at
-----------  -------------  -------------------
1            Ada            2026-02-01 10:00:00
2            Bo             2026-02-01 11:00:00
3            Cy             2026-02-02 10:00:00
4            Di             2026-02-02 11:00:00
5            Eli            2026-02-03 10:00:00

Expected result

delivery_id  corrected_customer_name
-----------  -----------------------
1            Bo
2            Ada
3            Di
4            Cy
5            Eli

Premium problem

This one's part of Premium. Unlock the full MySQL track plus every other premium problem on the site.

Write one SELECT query