0

Possible Duplicate:
Is the SQL WHERE clause short-circuit evaluated?

If we do

SELECT * FROM Business WHERE Latitude < 9 AND Latitude > 10

for example, will Latitude > 10 ever be called if Latitude is 8 for example?

What about in mysql?

What about in core data?

Is it dependent on SQL implementation or is there a definite standard that in where clause if one of the AND statement is false than the rest won't be executed.

I asked this to know whether I can save some computational power. Say the expression after the first AND is expensive to compute such as computing distance.

Community
  • 1
  • 1
user4951
  • 32,206
  • 53
  • 172
  • 282
  • Both sides of the `and` operator are evaluated. The query in your example would never return any rows, as a value can not be less than nine and more than ten at the same time. (I have just verified this in MySQL to be sure.) – Guffa Aug 01 '11 at 09:24

1 Answers1

1

WARNING: down-to-earth answer

You have probably mistaken AND with OR (in fact condition Latitude < 9 AND Latitude > 10 makes no sense..).

  • OR conjunction AFAIK in most languages this is implemented as follows: every condition is checked until one is true (i believe this is also true with SQL - EDIT: this answer suggests that the actual behaviour is implementation-dependant, i.e. you can't take that for sure in any RDBMS)
  • AND conjunction every condition is checked until one is false

So you probably wanted to write this condition:

Latitude < 9 OR Latitude > 10

which is equivalent to:

NOT(Latitude >= 9 AND Latitude <= 10)
Community
  • 1
  • 1
ascanio
  • 1,506
  • 1
  • 9
  • 18
  • 1
    Latitude < 9 OR Latitude > 10 is equivalent to: NOT( Latitude >= 9 AND Latitude <= 10 ) – niktrs Aug 01 '11 at 09:22
  • 1
    A small comment: Not, Not equal usually hurt performance, so "Latitude < 9 OR Latitude > 10" is more preferable – niktrs Aug 01 '11 at 09:35