Two statements needed using a temporary table: First: CREATE TABLE tmp1 AS SELECT id, (SELECT COUNT(*) FROM acc_trans WHERE chart_id IN (SELECT id FROM chart WHERE link LIKE '%tax%' AND acc_trans.trans_id = ar.id)) cnt FROM ar; Second: SELECT invnumber, amount, netamount FROM ar WHERE id IN (SELECT id FROM tmp1 WHERE cnt=0) AND transdate BETWEEN '2018-01-01' AND '2018-05-30'; Once you are done you can run: DROP TABLE tmp1;