Full answer
sql
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);The query as written:
sql
SELECT customer_id, status, COUNT(*) AS n FROM orders GROUP BY 1 ORDER BY 1;| customer_id | status | n |
|---|---|---|
| NULL | shipped | 1 |
| 1 | shipped | 2 |
| 2 | cancelled | 2 |
| 3 | shipped | 1 |
The original query was SELECT status, customer_id, COUNT(*) ... GROUP BY 1, meant to count orders per status. With customer_id moved first, it now groups by customer, and the status column shows a value from an arbitrary row of each group, which SQLite allows without warning.
A corrected version:
sql
SELECT status, COUNT(*) AS n FROM orders GROUP BY status ORDER BY status;| status | n |
|---|---|
| cancelled | 1 |
| pending | 1 |
| shipped | 4 |
Naming status makes the intent survive edits, and dropping the ungrouped customer_id removes the arbitrary value. PostgreSQL would have rejected the broken version, because status was neither grouped nor aggregated (not run here). Ordinals are handy in quick exploration but risky in saved reports.
sql
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 customer_id, status, COUNT(*) AS n FROM orders GROUP BY 1 ORDER BY 1;
SELECT status, COUNT(*) AS n FROM orders GROUP BY status ORDER BY status;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- Ordinals depend on select-list order
- Name grouping columns in saved queries
- SQLite allows ungrouped bare columns silently
Common mistakes
- Reordering columns without checking ordinals
- Trusting bare columns in grouped queries
Follow-ups
- Is ORDER BY 1 equally risky?
- How would a linter catch this?
Tips
- Point to the moved column