Fixing the Where/join order in the query