NBC
NBC

Reputation: 1698

Join new rows [SQL]

I have a left table with unique rows, and a right table that has those same unique rows + new unique ones. What's the right query to keep the unique rows from the left table (better quality data), and bring in only the new rows from the right table that don't exist in the left one already?

Upvotes: 0

Views: 52

Answers (3)

ssn
ssn

Reputation: 509

If you (for some reason) need to use joins instead of union, you could use a FULL JOIN and use ISNULL() or COALESCE() on each column.

This will leave everything from the left table plus everything from the right table that doesn't exist on the left.

Upvotes: 0

Cameron
Cameron

Reputation: 694

What about something like this?

SELECT a.id, a.data
FROM table_a
UNION ALL
SELECT b.id, b.data
FROM table_b
WHERE b.id NOT IN (SELECT id FROM table_a)

Upvotes: 2

Error_2646
Error_2646

Reputation: 3791

You could just UNION them. Depending on your DBMS, UNION will drop duplicates and UNION ALL will preserve them.

Upvotes: 1

Related Questions