When using SQL, are there any benefits of using =
in a WHERE
clause instead of LIKE
?
Without any special operators, LIKE
and =
are the same, right?
When using SQL, are there any benefits of using =
in a WHERE
clause instead of LIKE
?
Without any special operators, LIKE
and =
are the same, right?
Different Operators
LIKE
and=
are different operators. Most answers here focus on the wildcard support, which is not the only difference between these operators!=
is a comparison operator that operates on numbers and strings. When comparing strings, the comparison operator compares whole strings.LIKE
is a string operator that compares character by character.To complicate matters, both operators use a collation which can have important effects on the result of the comparison.
Motivating Example
Let's first identify an example where these operators produce obviously different results. Allow me to quote from the MySQL manual:
Please note that this page of the MySQL manual is called String Comparison Functions, and
=
is not discussed, which implies that=
is not strictly a string comparison function.How Does
=
Work?The SQL Standard § 8.2 describes how
=
compares strings:(Emphasis added.)
What does this mean? It means that when comparing strings, the
=
operator is just a thin wrapper around the current collation. A collation is a library that has various rules for comparing strings. Here's an example of a binary collation from MySQL:This particular collation happens to compare byte-by-byte (which is why it's called "binary" — it doesn't give any special meaning to strings). Other collations may provide more advanced comparisons.
For example, here is a UTF-8 collation that supports case-insensitive comparisons. The code is too long to paste here, but go to that link and read the body of
my_strnncollsp_utf8mb4()
. This collation can process multiple bytes at a time and it can apply various transforms (such as case insensitive comparison). The=
operator is completely abstracted from the vagaries of the collation.How Does
LIKE
Work?The SQL Standard § 8.5 describes how
LIKE
compares strings:(Emphasis added.)
This is pretty wordy, so let's break it down. Items ii and iii refer to the wildcards
_
and%
, respectively. IfP
does not contain any wildcards, then only item iv applies. This is the case of interest posed by the OP.In this case, it compares each "substring" (individual characters) in
M
against each substring inP
using the current collation.Conclusions
The bottom line is that when comparing strings,
=
compares the entire string whileLIKE
compares one character at a time. Both comparisons use the current collation. This difference leads to different results in some cases, as evidenced in the first example in this post.Which one should you use? Nobody can tell you that — you need to use the one that's correct for your use case. Don't prematurely optimize by switching comparison operators.
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.
LIKE
and=
are different.LIKE
is what you would use in a search query. It also allows wildcards like_
(simple character wildcard) and%
(multi-character wildcard).=
should be used if you want exact matches and it will be faster.This site explains
LIKE
Really it comes down to what you want the query to do. If you mean an exact match then use =. If you mean a fuzzier match, then use LIKE. Saying what you mean is usually a good policy with code.
In Oracle, a ‘like’ with no wildcards will return the same result as an ‘equals’, but could require additional processing. According to Tom Kyte, Oracle will treat a ‘like’ with no wildcards as an ‘equals’ when using literals, but not when using bind variables.
To address the original question regarding performance, it comes down to index utilization. When a simple table scan occurs, "LIKE" and "=" are identical. When indexes are involved, it depends on how the LIKE clause is formed. More specifically, what is the location of the wildcard(s)?
Consider the following:
There may be also negligible difference in the creation of the query plan when using "=" vs "LIKE".