SQL: Distinct values from two tables
Today must be database day for me...
A question on my local CFUG mailing list asks how to remove duplicate values from two different tables:
I have 2 tables that store email addresses. One table is for newsletters and the other is for registration to our site. I would like to make a list of all the email addresses we have but not show duplicate addresses. How do I go about doing this?
The answer -
UNION two SQL queries:
SELECT email FROM registration UNION SELECT email FROM newsletter
UNION will remove all the duplicates for you. If you wanted to show the duplicates as well, you would use
I figured I'd post this because
UNION is not used very often in SQL, so its easy to forget...
Like this? Follow me ↯Tweet Follow @pfreitag
SQL: Distinct values from two tables was first published on May 16, 2005.