CREATE TABLE txn (txn_id INT PRIMARY KEY AUTO_INCREMENT, account VARCHAR(64), amount INT); ALTER TABLE txn ADD INDEX i1 (account); #### INSERT INTO txn VALUES (NULL, '101', 40167); INSERT INTO txn VALUES (NULL, '101', 30821); ... #### INSERT INTO txn VALUES (NULL, '101', -131386); #### SELECT account, SUM(amount) as total FROM txn GROUP BY account HAVING total <> 0;