Move query statement to proper position