What if you could find any word or pattern in your data with just a simple symbol?
Why LIKE pattern matching in MySQL? - Purpose & Use Cases
Imagine you have a huge list of customer names in a notebook. You want to find all customers whose names start with 'Jo'. You have to flip through every page, reading each name carefully.
This manual search is slow and tiring. You might miss some names or make mistakes. It's hard to find patterns like 'names containing "son"' or 'names ending with "a"' without checking each entry one by one.
LIKE pattern matching lets you quickly search for text patterns inside your database. You can find all names starting with, ending with, or containing certain letters using simple symbols. It saves time and reduces errors.
SELECT * FROM customers WHERE name = 'Jo';SELECT * FROM customers WHERE name LIKE 'Jo%';It enables fast, flexible searches for text patterns in large databases without scanning every record manually.
A store owner wants to find all products with names containing 'book' to run a special discount. Using LIKE, they can quickly get the list without checking each product name.
Manual text searches are slow and error-prone.
LIKE pattern matching uses symbols like % and _ to find text patterns easily.
This makes searching large databases fast and accurate.
Practice
% symbol represent in a MySQL LIKE pattern?Solution
Step 1: Understand the role of % in LIKE
The % symbol in a LIKE pattern matches any sequence of characters, including an empty sequence.Step 2: Compare with other wildcard symbols
The _ symbol matches exactly one character, not a sequence. % is for zero or more characters.Final Answer:
Any sequence of zero or more characters -> Option AQuick Check:
% means any characters [OK]
- Confusing % with _
- Thinking % matches only one character
- Assuming % matches digits only
Solution
Step 1: Identify pattern for names starting with 'Jo'
Names starting with 'Jo' means 'Jo' followed by any characters, so pattern is 'Jo%'.Step 2: Check each option
WHERE name LIKE 'Jo%' uses 'Jo%' which matches strings starting with 'Jo'. Others do not match this pattern correctly.Final Answer:
WHERE name LIKE 'Jo%' -> Option AQuick Check:
Start with 'Jo' = 'Jo%' [OK]
- Putting % before 'Jo' instead of after
- Using _ instead of %
- Misplacing wildcards
users with a column email, what will this query return?SELECT email FROM users WHERE email LIKE '%@gmail._om';Solution
Step 1: Analyze the pattern '%@gmail._om'
The % matches any characters before '@gmail.', then _ matches exactly one character, then 'om' follows.Step 2: Understand what emails match
Emails ending with '@gmail.com', '@gmail.dom', '@gmail.xom' etc. match because _ matches one character before 'om'.Final Answer:
Emails ending with '@gmail.com' or '@gmail.dom' etc. (one char before 'om') -> Option CQuick Check:
_ matches one char before 'om' [OK]
- Thinking _ matches zero or many characters
- Ignoring the position of _ in pattern
- Assuming % matches only one character
SELECT * FROM products WHERE name LIKE 'TV_';What is the likely problem?
Solution
Step 1: Understand the pattern 'TV_'
The pattern 'TV_' matches strings starting with 'TV' and exactly one more character, so total length 3.Step 2: Reason why no rows returned
If no product names are exactly 3 characters starting with 'TV', no rows match. Longer names like 'TVS' or 'TV42' won't match.Final Answer:
The pattern 'TV_' matches only names with exactly 3 characters starting with 'TV' -> Option DQuick Check:
_ means exactly one char [OK]
- Thinking _ matches zero or many characters
- Using LIKE incorrectly with =
- Assuming 'TV_' matches longer names
Solution
Step 1: Understand the requirement
Phone numbers start with '555' and have exactly 10 digits total, so 3 fixed digits + 7 more digits.Step 2: Count underscores needed
Each underscore _ matches exactly one character. After '555', we need 7 underscores to make total length 10.Step 3: Check options
SELECT * FROM customers WHERE phone LIKE '555_______'; has '555' plus 7 underscores, matching exactly 10 characters. Others have wrong underscore counts or use % which allows variable length.Final Answer:
SELECT * FROM customers WHERE phone LIKE '555_______'; -> Option BQuick Check:
7 underscores after '555' = 10 chars total [OK]
- Using % which allows variable length
- Using wrong number of underscores
- Confusing _ with %
Ready to prove it?
Pass the test and this pattern moves unknown
