0
0
MySQLquery~10 mins

LIKE pattern matching in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - LIKE pattern matching
Start with string to match
Apply LIKE pattern
Check each character
Continue
End of string?
Match
The LIKE pattern matching checks a string against a pattern with wildcards, character by character, to find if it matches or not.
Execution Sample
MySQL
SELECT * FROM employees WHERE name LIKE 'J%n';
This query finds all employees whose names start with 'J' and end with 'n', with any characters in between.
Execution Table
StepString CheckedPattern PartMatch ResultAction
1'John''J%n'J matches JContinue
2'ohn''%n'% matches 'oh'Continue
3'n''n'n matches nContinue
4End of stringEnd of patternFull matchReturn row
5'Jason''J%n'J matches JContinue
6'ason''%n'% matches 'aso'Continue
7'n''n'n matches nContinue
8End of stringEnd of patternFull matchReturn row
9'Jim''J%n'J matches JContinue
10'im''%n'% matches 'im'Continue
11End of string'n'No matchSkip row
💡 Stop when string or pattern ends or no match found
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8After Step 9After Step 10After Step 11Final
Current String Position0134End013401EndEnd
Current Pattern Position0123End0123012End
Match StatusPendingMatchMatchMatchFull matchPendingMatchMatchFull matchPendingMatchNo matchNo match
Key Moments - 2 Insights
Why does the '%' wildcard match multiple characters?
Because '%' can match zero or more characters, it allows the pattern to skip over any number of characters in the string, as shown in steps 2 and 6 where '%n' matches 'oh' and 'aso'.
Why does the pattern fail to match 'Jim'?
At step 11, the pattern expects 'n' at the end, but the string ends with 'm', so the match fails as shown in the execution_table row 11.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the match result at step 6 for string 'Jason'?
A'%n' matches 'as'
B'%n' matches 'son'
C'%n' matches 'aso'
D'%n' matches 'on'
💡 Hint
Check the 'Pattern Part' and 'Match Result' columns at step 6 in the execution_table.
At which step does the pattern fail to match the string 'Jim'?
AStep 9
BStep 11
CStep 10
DStep 12
💡 Hint
Look at the 'Match Result' and 'Action' columns for 'Jim' in the execution_table.
If the pattern was 'J%n' and the string was 'Jan', what would happen at step 2?
A'%n' matches 'an'
B'%n' matches '' (empty string)
C'%n' matches 'a'
DNo match
💡 Hint
Remember '%' matches zero or more characters; check how it matched 'ohn' and 'ason' in previous examples.
Concept Snapshot
LIKE pattern matching in SQL:
- '%' matches zero or more characters
- '_' matches exactly one character
- Pattern is matched left to right
- Returns rows where string fits pattern
- Useful for flexible text search
Full Transcript
The LIKE pattern matching in MySQL checks if a string fits a pattern that can include wildcards. The '%' wildcard matches any number of characters, including none, while '_' matches exactly one character. The matching process compares the string and pattern character by character, moving forward when characters match or when '%' allows skipping characters. If the entire pattern matches the string, the row is returned. If a mismatch occurs, the row is skipped. For example, the pattern 'J%n' matches names starting with 'J' and ending with 'n', with anything in between. This is useful for searching text flexibly in databases.