Removed loops with queries. Fetchall query and in set comparator