MySQL
SQL Query
String Functions
Database Management
Text Search

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:

sql
SELECT * FROM employees WHERE name LIKE '%John%';

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:

sql
SELECT * FROM employees WHERE INSTR(name, 'John') > 0;

LOCATE()

LOCATE() also returns the position of the first occurrence. The only difference from INSTR() is argument order: LOCATE() takes the substring first.

sql
SELECT * FROM employees WHERE LOCATE('John', name) > 0;

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:

sql
SELECT * FROM employees WHERE name REGEXP 'John';

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.

  1. B-tree indexes: LIKE '%substring%' cannot use a standard B-tree index effectively because of the leading wildcard. INSTR() and LOCATE() wrap the column in a function, so they cannot use one either.
  2. Full-text indexes: available for InnoDB and MyISAM tables and designed for searching text columns:
sql
   CREATE FULLTEXT INDEX name_idx ON employees(name);
   SELECT * FROM employees WHERE MATCH(name) AGAINST('John');

Examples

String containment checks are most often used for filtering, validation, and pattern recognition.

Filtering rows

Employees whose title includes "Engineer":

sql
SELECT * FROM employees WHERE title LIKE '%Engineer%';

Matching a suffix

Emails that end in .edu:

sql
SELECT * FROM users WHERE email LIKE '%.edu';

Pattern matching with regex

Product codes containing three capital letters followed by two digits:

sql
SELECT * FROM orders WHERE product_code REGEXP '[A-Z]{3}[0-9]{2}';

Comparison

MethodDescriptionPerformanceUse Case
LIKEUses wildcards to match patternsBest for small tables Limited by wildcard usageSimple substring searches
INSTR()Finds the position of first substringSimilar to LIKE '%...%' cannot use an index on the columnMid-sized datasets With simple patterns
LOCATE()Similar to INSTR() with different syntaxSimilar to INSTR()Similar to INSTR()
REGEXPUses regular expressions for complex patternsPotentially slow on large tablesComplex pattern recognition
Full-Text IndexIndexes text columns for better performanceFast for text-heavy fieldsLarge datasets with search-heavy operations

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design