Full answer
Alphabetical sorting on a status column gives cancelled, pending, shipped. A support dashboard usually wants the open work first. ORDER BY accepts any expression, so map each status to a rank with CASE:
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);SELECT id, status, order_date
FROM orders
ORDER BY CASE status
WHEN 'pending' THEN 1
WHEN 'shipped' THEN 2
WHEN 'cancelled' THEN 3
ELSE 4
END,
order_date;| id | status | order_date |
|---|---|---|
| 105 | pending | 2026-03-09 |
| 101 | shipped | 2026-01-05 |
| 102 | shipped | 2026-02-11 |
| 104 | shipped | 2026-03-01 |
| 106 | shipped | 2026-03-12 |
| 103 | cancelled | 2026-02-14 |
The ELSE branch catches unexpected statuses, such as a new 'returned' value someone adds later, and pushes them to the end instead of making them disappear or jump to the top.
When the same order is used in many queries, a hard-coded CASE in each one becomes a maintenance problem. A small lookup table keeps the rank in one place and can carry labels for the UI too:
CREATE TABLE status_rank (status TEXT PRIMARY KEY, rank INTEGER NOT NULL);
INSERT INTO status_rank VALUES ('pending',1),('shipped',2),('cancelled',3);
SELECT o.id, o.status
FROM orders o
LEFT JOIN status_rank s ON s.status = o.status
ORDER BY COALESCE(s.rank, 99), o.id;| id | status |
|---|---|
| 105 | pending |
| 101 | shipped |
| 102 | shipped |
| 104 | shipped |
| 106 | shipped |
| 103 | cancelled |
The LEFT JOIN keeps orders whose status is missing from the lookup table, and COALESCE(s.rank, 99) sends them last. With an inner join those orders would vanish from the report, which is a worse failure than appearing in the wrong place.
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);
SELECT id, status, order_date
FROM orders
ORDER BY CASE status
WHEN 'pending' THEN 1
WHEN 'shipped' THEN 2
WHEN 'cancelled' THEN 3
ELSE 4
END,
order_date;
CREATE TABLE status_rank (status TEXT PRIMARY KEY, rank INTEGER NOT NULL);
INSERT INTO status_rank VALUES ('pending',1),('shipped',2),('cancelled',3);
SELECT o.id, o.status
FROM orders o
LEFT JOIN status_rank s ON s.status = o.status
ORDER BY COALESCE(s.rank, 99), o.id;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- ORDER BY accepts any expression, including CASE
- Map each value to a rank; use ELSE for unknown values
- For reusable orders, keep ranks in a lookup table
- Use a LEFT JOIN so unknown statuses are not dropped
Common mistakes
- Relying on alphabetical order for business states
- Using an inner join to the lookup table and losing rows with new statuses
Follow-ups
- How would you sort by priority and then by order date within each status?
- Could an enum type in PostgreSQL solve this?
Tips
- Mention the lookup-table version as the long-term design