select count(*) from theTable A inner join theTable B where (A.col_1 = x and B.col_1 = y and A.col_2 = B.col_2); should become SELECT COUNT(*) FROM theTable A JOIN theTable B ON (A.col_2 = B.col_2) WHERE A.col_1 = x AND B.col_1 = y