MySQL: check if a string contains a substring
Codemia Knowledge Hub ·
The simplest way is LIKE with % wildcards on both sides: WHERE name LIKE '%John%'. INSTR(), LOCATE() and REGEXP do the same job with different syntax, and a full-text index is the option to reach for on large, search-heavy tables.
Ways to test for a substring
LIKE with wildcards
LIKE is the most straightforward option. The percent sign (%) is a wildcard that matches zero or more characters, so a % on each side matches "John" anywhere in the value, and every row whose name contains "John" is returned:
INSTR()
INSTR() returns the position of the first occurrence of a substring, or 0 if it is not found. Any positive result means the substring is present:
LOCATE()
LOCATE() also returns the position of the first occurrence. The only difference from INSTR() is argument order: LOCATE() takes the substring first.
REGEXP
For complex patterns, REGEXP matches regular expressions. This example finds rows whose name contains "John", but the expression can be as elaborate as you need:
Performance on large tables
On small tables any method is fine. On large datasets the choice affects query speed, mostly through whether an index can be used.
- B-tree indexes:
LIKE '%substring%'cannot use a standard B-tree index effectively because of the leading wildcard.INSTR()andLOCATE()wrap the column in a function, so they cannot use one either. - Full-text indexes: available for InnoDB and MyISAM tables and designed for searching text columns:
Examples
String containment checks are most often used for filtering, validation, and pattern recognition.
Filtering rows
Employees whose title includes "Engineer":
Matching a suffix
Emails that end in .edu:
Pattern matching with regex
Product codes containing three capital letters followed by two digits:
Comparison
| Method | Description | Performance | Use Case |
LIKE | Uses wildcards to match patterns | Best for small tables Limited by wildcard usage | Simple substring searches |
INSTR() | Finds the position of first substring | Similar to LIKE '%...%'
cannot use an index on the column | Mid-sized datasets With simple patterns |
LOCATE() | Similar to INSTR() with different syntax | Similar to INSTR() | Similar to INSTR() |
REGEXP | Uses regular expressions for complex patterns | Potentially slow on large tables | Complex pattern recognition |
| Full-Text Index | Indexes text columns for better performance | Fast for text-heavy fields | Large datasets with search-heavy operations |
Related reading
- MySQL query to get column names?
- MySQL Query to select data from last week?
- MySQL Quick breakdown of the types of joins
- Mysql remote connect over ssh to a kubernetes pod
- MySQL remove all whitespaces from the entire column
- MySQL Removing Some Foreign keys
- Mysql Replication, 2 databases, 2 ways?
- MySQL Replication 3 masters, 1 Slave
System design interview problems on this topic
- Design a Distributed OLTP Database: solution and walkthrough
- Design a Wide Column Database: solution and walkthrough
- Design a Key Value Store: solution and walkthrough
- Design a Web Cache: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.