Text Search & Pattern Matching: LIKE, % and _ Wildcards & REGEXP in MySQL
Overview & Learning Objectives
Exact string equality (`=`) fails when searching unstructured text like product names, email addresses, or address notes. SQL provides powerful pattern matching via `LIKE` using `%` (multi-character wildcard) and `_` (single-character wildcard). Crucially, in MySQL's standard default collations (`utf8mb4_0900_ai_ci`), `LIKE` is case-insensitive by default. When case sensitivity is required, analysts use `BINARY LIKE`. For complex token matching, MySQL 8.0 provides full POSIX regular expressions via `REGEXP` and `RLIKE`.
- Search strings using `%` (zero or more characters) and `_` (exactly one character) wildcards.
- Understand why MySQL `LIKE` is case-insensitive by default in standard collations, and how to force case sensitivity with `BINARY`.
- Execute advanced regular expression pattern searches using `REGEXP` and `RLIKE`.
Industry Standard
Standard: Production standards at Flipkart and Swiggy index text search columns with Full-Text indexes or trigram patterns to maintain sub-millisecond search latency across 50 million catalog products.
Real-World Example: ScoopCraft Ice Cream Customer Search
ScoopCraft search customer records matching Gmail domains and specific SKU catalog formats.
| customer_name | Pattern Applied | Match Status | |
|---|---|---|---|
| Rahul Sharma | rahul.s@gmail.com | email LIKE '%@gmail.com' | MATCH |
| Ananya Sen | ananya.sen@outlook.com | name LIKE '%sharma%' | NO MATCH |
| Rohit Sharma | rohit.sharma@yahoo.com | name LIKE '%sharma%' | MATCH |
| priya sharma | priya@gmail.com | name LIKE '%sharma%' | MATCH (Case-insensitive in MySQL default) |
Functions & Syntax Covered in this Chapter
Here is the definition, execution syntax, parameter breakdown, and real-world business example for each function/syntax covered in this chapter:
LIKE (Wildcard Pattern Match)
Definition: Performs pattern matching using % (zero or more characters) and _ (exactly one character). In MySQL default collations (e.g. utf8mb4_0900_ai_ci), LIKE is case-insensitive.
SELECT * FROM orders WHERE customer_email LIKE "%@gmail.com";
%: Matches any string of zero or more arbitrary characters._: Matches exactly one single character position.
email LIKE '%@gmail.com'BINARY LIKE (Case-Sensitive Match)
Definition: Forces strict binary case-sensitive byte comparison in MySQL, overriding the default case-insensitive collation.
SELECT * FROM promo_codes WHERE BINARY code LIKE "SUMMER%";
BINARY: Type cast keyword forcing strict case comparison.
BINARY code LIKE 'SUMMER%'REGEXP / RLIKE (Regular Expression Match)
Definition: Performs POSIX regular expression matching against strings in MySQL 8.0 using ICU regex engine.
SELECT * FROM products WHERE product_name REGEXP "^[0-9]{3}-[A-Z]{2}";
pattern: Standard regex pattern (^ for start, $ for end, [0-9] for digits, | for OR).
sku REGEXP '^[0-9]{3}-'Common Mistakes & Limitations
- Searching for a literal `%` or `_` character without escaping (e.g. `WHERE discount LIKE "%10%"\%` without writing `ESCAPE`).
- Starting patterns with a leading wildcard (`LIKE "%keyword"`), which prevents MySQL from using B-Tree indexes and forces a full table scan.
Keyboard Shortcuts (MySQL Workbench)
| Action | Windows | Mac |
|---|---|---|
| Search in Query Editor | Ctrl + F | Cmd + F |
Pro Tips & Modern Best Practices
- If you need prefix matching, always write `LIKE "prefix%"` without a leading wildcard; MySQL can leverage B-Tree indexes for prefix lookups.
- Use `REGEXP` when checking for multiple alternative keywords: `WHERE title REGEXP "choco|mango|vanilla"`.