Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Union

Author  Topic 

mukhan85
Starting Member

46 Posts

Posted - 2008-06-20 : 10:39:18
Hi folks,
I have a question about the performance of the following query:

(SELECT name, lastName
from Data
where salary >= 2200
)
union

(SELECT name, lastName
from Data
where salary >= 2200
) union

(SELECT name, lastName
from Data
where salary >= 2200
)

How that query works? Is it slow for a larger data? Does it do 3 queries and then combine them?

Thank you.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-20 : 10:47:35
i've heard its better to use union all if you're sure the code chunks wont contain duplicate data. B/w why are you using same query thrice seperated by union? or was it just for illustration?
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-06-20 : 10:49:21
Union combines individual query results and remove duplicates. If you are sure that there won't be overlapping records between multiple resultset, you can use UNION ALL instead of UNION.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-06-20 : 18:15:43
And if you have such a query having different values for salary you should use


SELECT name, lastName
from Data
where (salary >= 2200 or salary >= ...)




Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -