Use '=' or LIKE to compare strings in SQL?

guerda picture guerda · Feb 5, 2009 · Viewed 601.1k times · Source

There's the (almost religious) discussion, if you should use LIKE or '=' to compare strings in SQL statements.

  • Are there reasons to use LIKE?
  • Are there reasons to use '='?
  • Performance? Readability?

Answer

soulmerge picture soulmerge · Feb 5, 2009

LIKE and the equality operator have different purposes, they don't do the same thing:
= is much faster, whereas LIKE can interpret wildcards. Use = wherever you can and LIKE wherever you must.

SELECT * FROM user WHERE login LIKE 'Test%';

Sample matches:

TestUser1
TestUser2
TestU
Test