WITH DuplicateCity AS (
SELECT Name
FROM city
GROUP BY Name
HAVING COUNT(Name) > 1
)
SELECT *
FROM city
WHERE Name IN (
SELECT Name
FROM DuplicateCity
)
ORDER BY Name
| ID | Name | CountryCode | District | Population | PopulationUpdated |
| 609 | Alexandria | EGY | Aleksandria | 5,263,542 | 2026-08-28 |
| 3,960 | Alexandria | USA | Virginia | 159,467 | 2026-08-28 |
| 1967 | Anyang | CHN | Henan | 1,146,839 | 2026-08-28 |
| 2341 | Anyang | KOR | Kyonggi | 595,644 | 2026-08-28 |
| 3,909 | Arlington | USA | Virginia | 207,627 | 2026-08-28 |
| 3,845 | Arlington | USA | Texas | 388,125 | 2026-08-28 |
| 3,941 | Aurora | USA | Illinois | 200,661 | 2026-08-28 |
| 3,853 | Aurora | USA | Colorado | 359,407 | 2026-08-28 |
| 3,546 | Barcelona | VEN | Anzoátegui | 815,141 | 2026-08-28 |
| 654 | Barcelona | ESP | Katalonia | 1,686,208 | 2026-08-28 |
| ... |
| Common Table Expressions (CTE) are an easy way to split a complex SQL query into multiple queries, which gives you more flexibility and manageability. This query is similar to the example with the IN operator. A subquery that selects duplicate cities is placed in a temporary table, which is then used in the main query. |