tags:

views:

114

answers:

6

I have a query:

select *
from randomtable
where randomnumber <> 0

The column "random_number" will never be a negative number.

So, if I write the query as:

select *
from randomtable
where randomnumber > 0

Is there any major difference?

+2  A: 

If it will never be less than zero, then NO

astander
A: 

What does 'greater than' mean? It means 'not equal' AND 'not less than'. In this case, if the number cannot be less than zero, 'greater than' is equivalent to 'not equal'.

Conrad Meyer
A: 

No, as it will always be above 0.

kevchadders
+4  A: 

No, there is no difference at all (in your specific situation). All numeric comparisons take the same time.

What's done at the lowest level is that zero is subtracted from randomnumber, and then the result is examined. The > operator looks for a positive non-zero result while the <> operator looks for a non-zero result. Those comparisons are trivial, and take the same amount of time to perform.

Guffa
Thanks for the details Guffa. I obviously have a more complex query that's causing me some issues, and this answers it perfectly for me.
Nicks
+1  A: 

The important thing is to determine how you know that random_number will never be a negative number. Is there a constraint that guarantees it? If not, what do you want your code to do if a bug somewhere else causes it to be negative?

Jeffrey Kemp
+1  A: 

The result set should never be different. The query path might be as the second might choose an index range scan starting at randomnumber=0 and looking for the records in sequence. As such the order of the results may differ.

If the order of the results is important, then put in an ORDER BY

Gary