Refactor the SQL statement