πŸŽ‰ 75% of content is free forever β€” Unlock Premium from $10/mo β†’
CW
Search courses…
πŸ’Ό 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?

DfLIKE Operator

The LIKE operator is a pattern matching predicate used in SQL WHERE clauses to search for a specified pattern in a column. It works with two special wildcard characters: % (percent) and _ (underscore).

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

DfWildcard Characters

SQL provides two primary wildcards: % represents zero, one, or multiple characters, and _ represents exactly one character. Together they enable powerful pattern-based queries.

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

Combine LIKE with AND/OR for complex pattern searches. You can also use NOT LIKE to exclude patterns.

-- 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____%';

LIKE queries cannot use standard indexes efficiently. For large tables, consider full-text search or specialized indexing for better performance.

Key Takeaways

  1. The LIKE operator enables flexible pattern matching in SQL queries
  2. % matches zero or more characters; _ matches exactly one character
  3. Use NOT LIKE to exclude patterns from results
  4. LIKE queries may not use indexes efficiently on large datasets
  5. Combine LIKE with AND/OR for complex filtering logic
⭐

Premium Content

LIKE Wildcards for Pattern Matching

Unlock this lesson and 900+ advanced tutorials with a Premium plan.

🎯End-to-end Projects
πŸ’ΌInterview Prep
πŸ“œCertificates
🀝Community Access

Already a member? Log in

Need Expert SQL Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement