SQL LIKE Pattern Matching
beginnerfiltering
2 min read
What is LIKE?
The LIKE operator performs pattern matching on text columns. It uses two wildcards:
%— Matches any sequence of characters (including none)._— Matches exactly one character.
Syntax
SELECT * FROM table_name
WHERE column LIKE pattern;PostgreSQL also offers ILIKE for case-insensitive matching.
When to Use
- Searching for names starting with a letter
- Finding emails from a specific domain
- Matching partial strings in a search feature
Key Points
- Case Sensitivity —
LIKEis case-sensitive in PostgreSQL. UseILIKEfor case-insensitive matching. - Leading Wildcards —
WHERE name LIKE '%son'cannot use a standard index. Consider a trigram index for better performance. - Escape Character — Use
ESCAPEto search for literal%or_. - NOT LIKE — Negates the pattern:
WHERE name NOT LIKE 'A%'. - Regex Alternative — PostgreSQL supports
~for full regex matching whenLIKEis not powerful enough.
Guided Practice
Solve the challenge below. Use hints when stuck and check your answer for instant feedback.
Practice challengeGuided learning mode
SQL LIKE Pattern Matching Challenge
Write a query that find all students whose first name starts with the letter A.
Expected result
Students with names like Alice, Anna, Andrew, etc.
Hidden checks
- Returned rows and values
- Output columns and result shape
- Final database state after the query runs
Lesson guidance
What is LIKE?
Initializing database...Each run starts from fresh sample data.
More Examples
Case-insensitive search
Find products with 'pro' anywhere in the name, regardless of case.
Initializing database...Each run starts from fresh sample data.
Single character wildcard
Find grades that are exactly one character.
Initializing database...Each run starts from fresh sample data.
Frequently Asked Questions
What is the difference between LIKE and ILIKE?
LIKE is case-sensitive: 'Alice' LIKE 'a%' is false. ILIKE is case-insensitive: 'Alice' ILIKE 'a%' is true. ILIKE is a PostgreSQL extension.
How do I search for a literal percent sign?
Use the ESCAPE clause: WHERE column LIKE '10\%' ESCAPE '\'. This searches for the literal string '10%'.