Mastering MySQL for Data AnalystsLesson 12 of 42
Lesson 12 of 42

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_nameemailPattern AppliedMatch Status
Rahul Sharmarahul.s@gmail.comemail LIKE '%@gmail.com'MATCH
Ananya Senananya.sen@outlook.comname LIKE '%sharma%'NO MATCH
Rohit Sharmarohit.sharma@yahoo.comname LIKE '%sharma%'MATCH
priya sharmapriya@gmail.comname 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:

Feature 1

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.

Syntax / Code Pattern:
SELECT * FROM orders WHERE customer_email LIKE "%@gmail.com";
Parameters Breakdown:
  • %: Matches any string of zero or more arbitrary characters.
  • _: Matches exactly one single character position.
Real-World Example (ScoopCraft Ice Cream Customer Search):
Problem / Scenario: Find all customers with Gmail addresses in the order records.
SQL Query / Expression: email LIKE '%@gmail.com'
Result: Matches rahul.s@gmail.com, priya@gmail.com
Why it works: The % wildcard matches any username prefix ending with @gmail.com.
Feature 2

BINARY LIKE (Case-Sensitive Match)

Definition: Forces strict binary case-sensitive byte comparison in MySQL, overriding the default case-insensitive collation.

Syntax / Code Pattern:
SELECT * FROM promo_codes WHERE BINARY code LIKE "SUMMER%";
Parameters Breakdown:
  • BINARY: Type cast keyword forcing strict case comparison.
Real-World Example (Voucher Code Case Verification):
Problem / Scenario: Match uppercase voucher code "SUMMER50" while rejecting lowercase "summer50".
SQL Query / Expression: BINARY code LIKE 'SUMMER%'
Result: Matches SUMMER50, rejects summer50
Why it works: BINARY converts the string to binary bytes for exact ASCII case comparison.
Feature 3

REGEXP / RLIKE (Regular Expression Match)

Definition: Performs POSIX regular expression matching against strings in MySQL 8.0 using ICU regex engine.

Syntax / Code Pattern:
SELECT * FROM products WHERE product_name REGEXP "^[0-9]{3}-[A-Z]{2}";
Parameters Breakdown:
  • pattern: Standard regex pattern (^ for start, $ for end, [0-9] for digits, | for OR).
Real-World Example (SKU Code Validation):
Problem / Scenario: Find products whose SKU begins with 3 digits followed by a hyphen.
SQL Query / Expression: sku REGEXP '^[0-9]{3}-'
Result: Matches 501-VAN, 802-CHO
Why it works: Anchors match to start of string with exactly 3 digits followed by hyphen.

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)

ActionWindowsMac
Search in Query EditorCtrl + FCmd + 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"`.