Selecting distinct values from two tables

13,508

Solution 1

You can UNION two full sets in a subquery and then select DISTINCT col from that.

Something like:

SELECT DISTINCT col FROM (SELECT * FROM tbl1 UNION ALL SELECT * FROM tbl2)

Solution 2

You can use

UNION ALL

statement. It doesn't remove duplicate rows so you can see if there are any duplicates.

Share:
13,508
Tucker
Author by

Tucker

upvote

Updated on June 04, 2022

Comments

  • Tucker
    Tucker almost 2 years

    I have two rather large databases (+1 million rows each). Both tables have the same structure.

    How can I check if each value in a column is unique across both tables?

    Is there a
    SELECT COUNT(DISTINCTcol) FROM tbl
    type of query that will consider BOTH tables?

    Thanks!