Crinsane
Crinsane

Reputation: 818

Is there a difference between these two queries?

Well, pretty simple question I think, but I can't find a satisfying answer. Is there a difference between these two queries?

SELECT * FROM table WHERE column1 = 'x' OR column2 = 'x'

SELECT * FROM table WHERE (column1 = 'x' OR column2 = 'x')

Thanks in advance

Upvotes: 0

Views: 137

Answers (5)

zessx
zessx

Reputation: 68790

No, there's not a single difference.

See more MySQL Syntax here.

Upvotes: 1

Fahim Parkar
Fahim Parkar

Reputation: 31627

Right now there is NO difference in your queries.

There would be difference if there would be AND condition as addition in your query.

e.g

SELECT * FROM table WHERE id=1 AND column1 = 'x' OR column2 = 'x'

and

SELECT * FROM table WHERE id=1 AND (column1 = 'x' OR column2 = 'x')

Note

SELECT * FROM table WHERE id=1 AND column1 = 'x' OR column2 = 'x'

would be equivalent to

SELECT * FROM table WHERE (id=1 AND column1 = 'x') OR column2 = 'x'

Hope you get the difference.

Upvotes: 2

Romil Kumar Jain
Romil Kumar Jain

Reputation: 20745

The role of parenthesis will be usefull when there is one more condition with AND operator. otherwise no difference in execution plan except syntax.

Upvotes: 3

eggyal
eggyal

Reputation: 125835

Further to my comment above, the manual explains:

Some of the optimizations performed by MySQL follow:

  • Removal of unnecessary parentheses:

       ((a AND b) AND c OR (((a AND b) AND (c AND d))))
    -> (a AND b AND c) OR (a AND b AND c AND d)

So no, there is no difference whatsoever.

Upvotes: 2

Kshitij
Kshitij

Reputation: 8614

No. its the same. As long as there are only two conditions braces doesn't have much significance.

Upvotes: 1

Related Questions