fan-trap.sql
1CREATE TABLE orders (id INT PRIMARY KEY);2CREATE TABLE payments (id INT PRIMARY KEY, order_id INT, amount INT);3CREATE TABLE refunds (id INT PRIMARY KEY, order_id INT);4 5SELECT o.id, SUM(p.amount) AS paid6FROM orders o7JOIN payments p ON p.order_id = o.id8JOIN refunds r ON r.order_id = o.id9GROUP BY o.id;
Terminal output excerpt
$ sqlassert suggest fan-trap.sql
Fan trap · line 9
SUM(p.amount) reads rows that JOIN refunds AS r may repeat: the join is not proved to repeat rows of only one input (fan trap), so a value may be counted more than once