When using SQL, are there any benefits of using =
in a WHERE
clause instead of LIKE
?
Without any special operators, LIKE
One difference - apart from the possibility to use wildcards with LIKE - is in trailing spaces: The = operator ignores trailing space, but LIKE does not.
Depends on the database system.
Generally with no special characters, yes, = and LIKE are the same.
Some database systems, however, may treat collation settings differently with the different operators.
For instance, in MySQL comparisons with = on strings is always case-insensitive by default, so LIKE without special characters is the same. On some other RDBMS's LIKE is case-insensitive while = is not.
This is a copy/paste of another answer of mine for question SQL 'like' vs '=' performance:
A personal example using mysql 5.5: I had an inner join between 2 tables, one of 3 million rows and one of 10 thousand rows.
When using a like on an index as below(no wildcards), it took about 30 seconds:
where login like '12345678'
using 'explain' I get:
When using an '=' on the same query, it took about 0.1 seconds:
where login ='12345678'
Using 'explain' I get:
As you can see, the like
completely cancelled the index seek, so query took 300 times more time.