title: MySQL getting duplicates from select distinct + union
body: <p>When I run the following query in MySQL I get lots of duplicates. I know I've been more than clear enough that I just need distinct records, so I can't understand why it would double it up for me. It seems that all duplicates appears when I include the last union(<code>importorders</code> table) since most customers have the same address in customers and orders. Can anyone help me understand why this happends?</p>
  
  <pre><code>SELECT DISTINCT PostalCode, City, Region, Country
  FROM 
  (select distinct postalcode, city, region, country
  from importemployees
  UNION
  select distinct postalcode, city, region, country
  from importcustomers
  UNION
  select distinct postalcode, city, region, country
  from importproducts
  UNION
  select distinct shippostalcode as postalcode, shipcity as city, shipregion as region, shipcountry as country
  from importorders) T
  </code></pre>
  
  <hr>
  
  <p><img src="https://i.stack.imgur.com/gNuL4.jpg" alt="Query and result"></p>
  
  <p>As you can see. Some rows are duplicates.</p>
  
  <p>If I use <code>INSERT IGNORE</code> to insert <code>importcustomers</code> first, then <code>importorders</code>, then it manages to identify the records as duplicates. Why doesn't the select-query work?</p>