Fix order for sql query