ADVERTISEMENT

SQL LIKE & Wildcards

The LIKE operator is used to search for patterns in text. It works with wildcards to match partial strings.

🔹 Wildcards You Can Use

WildcardMeaning
%Matches zero or more characters
_Matches exactly one character

🔹 Examples with LIKE

-- Starts with 'A'
SELECT * FROM employees WHERE name LIKE 'A%';

-- Ends with 'n'
SELECT * FROM employees WHERE name LIKE '%n';

-- Contains 'ar'
SELECT * FROM employees WHERE name LIKE '%ar%';

-- Second letter is 'e'
SELECT * FROM employees WHERE name LIKE '_e%';

-- 5-letter names only
SELECT * FROM employees WHERE name LIKE '_____';

🔹 Case Sensitivity

  • MySQL: LIKE is case-insensitive by default (depends on collation)
  • PostgreSQL / SQL Server / Oracle: Usually case-sensitive
    • Use ILIKE in PostgreSQL for case-insensitive match

🧠 Quick Recap

PatternMatches Example
'J%'John, Jack, Jenny
'%son'Jason, Nelson
'__a%'Anna, Sara, Mark
'_____'Any 5-letter name

✅ Great for flexible searches like names, emails, and codes

ADVERTISEMENT