locked
Get distinct results across two similar tables RRS feed

  • Question

  • I have two tables Table1 and Table2 with the same columns A, B, and C.  Some records exist only in Table1, some records exist only in Table2, and some combinations of A, B, and C exist in both tables.  I'd like a query that will give me the DISTINCT records from both.  This is what I came up with but I have feeling there's a cleaner way with a JOIN.  Suggestions?

    SELECT DISTINCT A, B, C
      FROM (
    	   SELECT A,
    			B,
    			C
    		FROM Table1
    	   UNION ALL
    	   SELECT A,
    			B,
    			C
    		FROM Table2)

    Friday, August 2, 2013 5:49 PM

Answers


  • SELECT A,
    			B,
    			C
    		FROM Table1
    	   UNION 
    	   SELECT A,
    			B,
    			C
    		FROM Table2


    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    • Marked as answer by Matt S- Friday, August 2, 2013 5:54 PM
    Friday, August 2, 2013 5:53 PM

All replies


  • SELECT A,
    			B,
    			C
    		FROM Table1
    	   UNION 
    	   SELECT A,
    			B,
    			C
    		FROM Table2


    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    • Marked as answer by Matt S- Friday, August 2, 2013 5:54 PM
    Friday, August 2, 2013 5:53 PM

  • SELECT A,
    			B,
    			C
    		FROM Table1
    	   UNION 
    	   SELECT A,
    			B,
    			C
    		FROM Table2


    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    Doh!
    Friday, August 2, 2013 5:54 PM