Sql – For an Oracle NUMBER datatype, LIKE operator vs BETWEEN..AND operator

oraclesql

Assume mytable is an Oracle table and it has a field called id. The datatype of id is NUMBER(8). Compare the following queries:

select * from mytable where id like '715%'

and

select * from mytable where id between 71500000 and 71599999

I would think the second is more efficient since I think "number comparison" would require fewer number of assembly language instructions than "string comparison". I need a confirmation or correction. Please confirm/correct and throw any further comment related to either operator.

UPDATE: I forgot to mention 1 important piece of info. id in this case must be an 8-digit number.

Best Answer

If you only want values between 71500000 and 71599999 then yes the second one is much more efficient. The first one would also return values between 7150-7159, 71500-71599 etc. and so forth. You would either need to sift through unecessary results or write another couple lines of code to filter the rest of them out. The second option is definitely more efficient for what you seem to want to do.