Most Efficient Way To Get Distinct Values

I need to get all distinct values from 10 different tables that exist in the field [favoritesport]  And each table holds close to 50K records so I am looking at 500,000 records to get distinct values for.  Would the fastest, less intrusive way of achieving this be to just create a UNION ALL so run 

Select Distinct([favoritesport]) from table1
Union
Select Distinct([favoritesport]) from table2
Union
Select Distinct([favoritesport]) from table3
etc etc etc

August 19th, 2015 4:45pm

UNION already does DISTINCT so you don't need DISTINCT keyword in your queries. Other than that, I don't think a better method exists. You can start from the table having the most number of rows and do RIGHT JOIN with all other tables, but I don't think it will be any faster.
Free Windows Admin Tool Kit Click here and download it now
August 19th, 2015 4:52pm

UNION already does DISTINCT so you don't need DISTINCT keyword in your queries. Other than that, I don't think a better method exists. You can start from the table having the most number of rows and do RIGHT JOIN with all other tables, but I don't think it will be a
August 19th, 2015 4:53pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics