UPDATESQL

Unit 5, lesson 3 · CASE in ORDER BY

Sorting by your own rules

Orders that need attention first

ORDER BY status sorts alphabetically: cancelled, delivered, returned, shipped. When the order you want is not alphabetical or numeric, turn each value into a number with CASE and sort by that. The CASE goes straight into ORDER BY and does not need to be in SELECT. When the only test is whether one column equals a value, there is a shorter form: CASE status WHEN 'shipped' THEN 1 ... END.

Example
SELECT name, category
FROM products
ORDER BY CASE category
           WHEN 'Home office' THEN 1
           WHEN 'Audio' THEN 2
           ELSE 3
         END,
         name;

Your task

Support wants November's orders in the order they should be looked at: cancelled orders first (refunds to confirm), then shipped (still on the way), then delivered. Within each status, newest first, and orders from the same day by id. Return the id, order_date and status for orders placed from November 1, 2026.

Tables you will use: orders

Try it in your browserFree, no sign-up. Your query is checked as you go.