Refactor query: remove duplicates.