Sameer
Sameer

Reputation: 548

OR Operator used in parenthesis along with where clause returns different result.

What is the difference between the below two SQL statements:

SELECT * FROM Customers
WHERE Country='France' OR Country='Mexico'

and

SELECT * FROM Customers
WHERE (Country='France' OR Country='Mexico')

Why do these two statements return different results?

Upvotes: 1

Views: 65

Answers (2)

podiluska
podiluska

Reputation: 51494

There is no difference.

declare @t table (id int identity (1,1), country varchar(20), b int)

insert @t (country, b) values ('france',1),('france',2),('france',3),('mexico',1),('mexico',4), ('brazil',1)

select * from @t where (country = 'france' or country = 'mexico') 
select * from @t where country = 'france' or country = 'mexico'
-- both return rows 1-5

However, if you add a further clause (eg AND) then you need to consider which clauses take priority.

select * from @t 
where (country = 'france' or country = 'mexico') and b = 1

-- returns  rows 1 and 4

select * from @t 
where country = 'france' or country = 'mexico'  and b = 1

-- returns rows 1-4

Upvotes: 3

sdrzymala
sdrzymala

Reputation: 387

There is no difference in this query and this two queries should return the same results - i've event tested this. It's for sure. SQL where clouse works according to logic laws: http://en.wikipedia.org/wiki/Propositional_logic#Basic_and_derived_argument_forms

So the brackets in this case do nothing.

Upvotes: 0

Related Questions