Iterate over query statement instead of array