Skip to main content
Ben Nadel at CFUNITED 2009 (Lansdowne, VA) with: Phill Nacelli and Amy Nacelli
Ben Nadel at CFUNITED 2009 (Lansdowne, VA) with: Phill Nacelli Amy Nacelli

SQL AND / OR Order of Operations

By
Published in Comments (5)

When is comes to SQL and a series of AND and OR directives in the WHERE clause, the order of operations can be confusing. I am not sure what the rules are to this, but this is what my personal experience has shown me. Even with no parenthesis, OR clauses create the same effect as adding parentheses. So for instance, the SQL query below:

SELECT
	name
FROM
	tag
WHERE
	1 = 0
AND
	1 = 0
OR
	1 = 1
AND
	1 = 0
AND
	2 = 2
OR
	1 = 1
AND
	2 = 2

... seems to be rewritten to be the same as:

SELECT
	name
FROM
	tag
WHERE
	(
			1 = 0
		AND
			1 = 0
	)
OR
	(
			1 = 1
		AND
			1 = 0
		AND
			2 = 2
	)
OR
	(
			1 = 1
		AND
			2 = 2
	)

Notice that the OR directive seem to just group the other directives (AND) in the where clause. That is why changing the last AND 2 = 2 to AND 2 = 3 will turn the whole WHERE clause to be untrue. So, even though the following looks like it would be true due to the OR 1 = 1 directives:

SELECT
	name
FROM
	tag
WHERE
	1 = 0
AND
	1 = 0
OR
	1 = 1
AND
	1 = 0
AND
	2 = 2

-- Might think this OR directive will make everything true
-- but it does not (see below).
OR
	1 = 1

AND
	2 = 3

... it turns out to be false when you take in the behind-the-scenes parentheses into account:

SELECT
	name
FROM
	tag
WHERE
	(
			1 = 0
		AND
			1 = 0
	)
OR
	(
			1 = 1
		AND
			1 = 0
		AND
			2 = 2
	)

-- This OR directive no longer returns true since you can see
-- that it is not just OR 1 = 1, but in fact, it is an OR'ing
-- of two separate AND directives that do not return true.
OR
	(
			1 = 1
		AND
			2 = 3
	)

So, as far as order of operations go, it's not so much about AND and OR directives in the WHERE statement, but rather, how the behind-the-scenes parentheses will group the non-OR directives. In order for this to be clear, I would suggest always using the parenthesis when using AND and OR statements. Furthermore, even if it is not required, the parenthesis will make it easier for anyone else looking at your code to understand what is going on.

Want to use code from this post? Check out the license.

Reader Comments

1 Comments

It's simple from the boolean logic point of view. AND is multiplication whereas OR is addition.

AND:
1 AND 1 = 1 <=> 1 * 1 = 1
0 AND 1 = 0 <=> 0 * 1 = 0
etc.

OR:
0 OR 0 = 0 <=> 0 + 0 = 0
0 OR 1 = 1 <=> 0 + 1 = 1
etc.

The only exception is
1 OR 1 = 1 <=> 1 + 1 = 1
but it makes sense because there is no value above 1.

The ordering of operations is the same as the order of simple arithmetic.

1 Comments

Thanks a lot Ben for this article. It cleared my confusion on whether AND operations are performed first or OR when there's no parenthesis. :)

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel