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 SUM(status = 'shipped') / COUNT(*) * 100 AS pct_shipped FROM orders;| pct_shipped |
|---|
| 0 |
SUM(status = 'shipped') is 4 and COUNT(*) is 6. Integer division truncates 4 / 6 to 0 before the multiplication happens, so the order of operations makes things worse.
A corrected version:
sql
SELECT ROUND(100.0 * SUM(status = 'shipped') / COUNT(*), 1) AS pct_shipped FROM orders;| pct_shipped |
|---|
| 66.7 |
Starting with 100.0 makes the whole expression real arithmetic, giving 66.7. In PostgreSQL, status = 'shipped' is a boolean that SUM rejects, so write COUNT(*) FILTER (WHERE status = 'shipped') or SUM(CASE WHEN status = 'shipped' THEN 1 ELSE 0 END) there. The PostgreSQL run with FILTER returns the same value:
sql
SELECT ROUND(100.0 * COUNT(*) FILTER (WHERE status = 'shipped') / COUNT(*), 1) AS pct_shipped FROM orders;| pct_shipped |
|---|
| 66.7 |
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 SUM(status = 'shipped') / COUNT(*) * 100 AS pct_shipped FROM orders;
SELECT ROUND(100.0 * SUM(status = 'shipped') / COUNT(*), 1) AS pct_shipped FROM orders;Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.
Key points
- Integer / integer truncates
- Put the 100.0 first
- PostgreSQL needs FILTER or CASE instead of summing booleans
Common mistakes
- Multiplying by 100 after the division
- Casting the final result, which is already 0
Follow-ups
- How would you show the percentage per customer?
- Why might NUMERIC be preferred over float here?
Tips
- Show where truncation happens