🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
đŸ’ŧ Servicesâ„šī¸ Aboutâœ‰ī¸ ContactView Pricing Plansfrom $10

LIKE Wildcards for Pattern Matching

SQL PatternsLIKE WildcardsđŸŸĸ Free Lesson

Advertisement

SQL Patterns

LIKE Wildcards for Pattern Matching

Pattern matching is essential for flexible data searching when exact matches aren't enough.

  • % Wildcard — matches zero or more characters in a string
  • _ Wildcard — matches exactly one single character Master flexible searching across your data.

What is the LIKE Operator?

Pattern Matching with Wildcards% WildcardMatches 0+ charactersJ% → James, J, Jo%son → Johnson, Son%art% → Arthur, PartyStarts / Ends / ContainsCannot use index efficientlyUnderscore WildcardMatches exactly 1 charA underscore AB, A1, Axunderscoreat Cat, Bat, Sat3x underscore Bob, Ann, MaxFixed-length patternsUse for exact char countQuick ReferencePattern → Match'J%' → starts with J'%son' → ends with son3x underscore → exactly 3 charsS + 2x underscore% → S + 2+ charsConsider full-text searchfor large datasets
-- Find all customers whose names start with 'J'
SELECT first_name, last_name
FROM customers
WHERE first_name LIKE 'J%';

-- Find all products with 'phone' anywhere in the name
SELECT product_name
FROM products
WHERE product_name LIKE '%phone%';

-- Find all emails ending with '.com'
SELECT email
FROM users
WHERE email LIKE '%.com';

Wildcard Characters

WildcardDescriptionExampleMatches
%Zero or more characters'A%'Apple, A, ABC
_Exactly one character'A_'AB, A1, Ax
[abc]Any character in set'[AE]%'Apple, Egg
[^abc]Any character NOT in set'[^A]%'Banana, Cherry
[a-z]Range of characters'[a-c]%'Apple, Banana
-- Find all employees whose names start with 'A'
SELECT first_name
FROM employees
WHERE first_name LIKE 'A%';

-- Find products with exactly 5 characters
SELECT product_code
FROM products
WHERE product_code LIKE '_____';

-- Find names starting with 'S' followed by any two characters
SELECT customer_name
FROM customers
WHERE customer_name LIKE 'S__%';

Combining LIKE with Other Operators

-- Find customers whose email starts with 'info' and ends with '.org'
SELECT email
FROM contacts
WHERE email LIKE 'info%org';

-- Find employees NOT in departments starting with 'S'
SELECT first_name, last_name
FROM employees
WHERE department LIKE 'S%' AND salary > 50000;

-- Find products matching pattern OR price threshold
SELECT product_name, price
FROM products
WHERE product_name LIKE '%pro%' OR price > 100;

Pattern Matching Examples

PatternDescriptionSample MatchSample Non-Match
'J%'Starts with JJamesAdam
'%son'Ends with sonJohnsonSmith
'%art%'Contains artArthurBaker
'_at'3 chars ending in catCatBat
'___'Exactly 3 charsBobDavid
-- Find all order numbers containing '2024'
SELECT order_id, order_date
FROM orders
WHERE order_id LIKE '%2024%';

-- Find phone numbers with country code +1
SELECT phone_number
FROM contacts
WHERE phone_number LIKE '+1%';

-- Find product codes starting with 'PROD' followed by digits
SELECT product_code
FROM inventory
WHERE product_code LIKE 'PROD____%';

Need Expert SQL Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement