MySQL query String contains
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
MySQL is a widely used open-source relational database management system. One common task when working with MySQL is to query data based on whether a string contains certain substrings. In this article, we will explore different methods to perform string containment checks in MySQL using various functions and operators. Additionally, we will cover performance considerations and use cases.
Methods for String Containment Checks
Using LIKE Operator
The LIKE operator is one of the most straightforward methods for checking if a string contains a substring in MySQL. The percent sign (%) acts as a wildcard, matching zero or more characters.
In this example, any record where the name field contains "John" will be returned. The use of % before and after "John" allows the query to match any occurrence of "John" within the string.
Using INSTR() Function
The INSTR() function returns the position of the first occurrence of a substring within a string. If the substring is not found, it returns 0.
This query will return records where "John" is part of the name, as INSTR() will return a positive integer indicating its position.
Using LOCATE() Function
Functionally similar to INSTR(), the LOCATE() function returns the position of the first occurrence of a substring.
The difference between INSTR() and LOCATE() lies in the order of arguments; LOCATE() takes the substring first.
Using REGEXP Operator
For complex pattern matching, the REGEXP operator allows regular expressions, providing a powerful way to search strings.
This example finds records where name contains the pattern "John". Regular expressions can be crafted to match much more complex patterns if necessary.
Performance Considerations
When performing string searches in MySQL, especially on large datasets, performance can become an issue. The choice of method can affect query speed.
- Indexes: While basic indices may speed up string searches, methods like
LIKE '%substring%'can't effectively use standard B-tree indexes because of the leading wildcard. Full-text indexes can be beneficial for text-based searches. - Use of Full-Text Indexes: Available in InnoDB and MyISAM tables, full-text indexing is tailored for match searches on a text column.
- Substring Functions: Functions like
INSTR()andLOCATE()are marginally faster thanLIKEsearches without leading%because they don't trigger full table scans when indexed properly.
Examples and Use Cases
Filtering Data
Often, string containment checks help filter database entries. For example, fetching employees whose titles include "Engineer".
Validation
Ensuring data integrity sometimes involves validation against known substrings.
This query examines if emails belong to educational institutions without capturing malformed entries.
Pattern Recognition
Using regex, complex patterns can be searched, such as finding records with specific alphanumeric combinations:
Summary Table
Below is a summary of key points regarding string containment checks in MySQL:
| 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 | Slightly faster due to direct substring identification | 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 unless indexed | Complex pattern recognition |
| Full-Text Index | Indexes text columns for better performance | Fast for text-heavy fields | Large datasets with search-heavy operations |
Understanding the appropriate scenario to use each method allows for efficient query writing and database performance optimization. By leveraging the correct function or operator, you can ensure your MySQL queries remain both effective and performant.
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 Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.