The MySQL LIKE operator searches string columns for partial text patterns inside WHERE clauses. Instead of requiring an exact match like =, LIKE lets you match prefixes, suffixes, or substrings using wildcards.
LIKE supports two pattern wildcards:
% (Percent): Matches zero, one, or many characters of any kind._ (Underscore): Matches exactly one single character.SELECT column1, column2
FROM table_name
WHERE text_column LIKE 'pattern%';
The queries below search demo_customers by email domain and name prefix:
-- Prefix match (can use a B-tree index)
SELECT id, first_name, email
FROM demo_customers
WHERE first_name LIKE 'Al%';
-- Suffix / domain match
SELECT id, first_name, email
FROM demo_customers
WHERE email LIKE '%@example.com';
-- Exclude test accounts
SELECT id, first_name, email
FROM demo_customers
WHERE email NOT LIKE '%+test%';
| id | first_name | |
|---|---|---|
| 3 | Alice | [email protected] |
| 9 | Alan | [email protected] |
LIKE 'alex%' can use a B-tree index range scan, whereas LIKE '%alex%' forces a full table scan on every row. For fast substring search on large tables, use a MySQL FULLTEXT index (MATCH ... AGAINST)._ci collations (such as utf8mb4_0900_ai_ci), LIKE is case-insensitive. Use LIKE BINARY or a _cs/_bin collation when exact casing matters.% and _: To search for a literal 50% discount string, escape the percent sign: WHERE note LIKE '%50\%%'.LIKE case-sensitive in MySQL?By default, no—most MySQL collations end in _ci (case-insensitive), so 'al%' matches both 'Alice' and 'alice'.
FULLTEXT instead of LIKE '%word%'?Switch to FULLTEXT indexes whenever you search descriptions, articles, or logs with more than a few thousand rows.
LIKE work on numeric or date columns?MySQL implicitly casts numbers or dates to strings for LIKE, but doing so disables indexes; use range comparisons like >= and < on dates instead.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.