Get unique data across certain column in multiple tables – sql

I know this is probably so odd to ask. But lets say I have 3 tables:

Table 1

ID Name
1 Adam
2 David
3 Conor

Table 2

ID Name
1 Adam
2 Derek
3 Niall

Table 3

ID Name
1 Adam
2 David
3 John

Is there any way I can write a query to get the unique names across all 3 tables. So it would return “Adam, David, Conor, Derek, Niall, John”

Order doesn’t matter

If it helps, all name values are related to a names table

Answer

yes , one way is to union them

select name from table1
union 
select name from table2
union 
select name from table3

union automatically removes duplicate cases

Leave a Reply

Your email address will not be published. Required fields are marked *