PostgreSQL LIKE
The LIKE
operator is used for pattern matching in string searches. Wildcards %
and _
help match partial strings.
Key Topics
1. LIKE Syntax
SELECT *
FROM employees
WHERE first_name LIKE 'Jo%';
This finds any first_name starting with Jo
(like John, Joe, Josh, etc.).
2. Wildcards
- %: Matches zero or more characters.
- _: Matches exactly one character.
Best Practices
- Use appropriate indexes, but note that leading wildcards can hinder index use (e.g.,
%test
). - Consider
ILIKE
(case-insensitive) for more flexible matching.
Key Takeaways
LIKE
uses wildcards to match patterns in text-based columns.ILIKE
is a case-insensitive variant in PostgreSQL.