LIKE — Pattern Matching
The LIKE keyword allows you to filter rows based on a pattern.
It is used with text values (strings) and supports wildcard characters.
Syntax
SELECT column1, column2, ... FROM table_name
WHERE column LIKE 'pattern';
Wildcards:
| Wildcard | Meaning |
|---|---|
% | Matches zero or more characters |
_ | Matches exactly one character |
Example 1: Names starting with 'A'
SELECT name FROM employees
WHERE name LIKE 'A%';
This returns all employees whose name starts with 'A'.
Example 2: Names ending with 'a'
SELECT name FROM employees
WHERE name LIKE '%a';
This returns all employees whose name ends with 'a'.
Example 3: Names with 4 letters only
SELECT name FROM employees
WHERE name LIKE '____';
This returns names that are exactly 4 characters long.
Example 4: Names containing 'ar'
SELECT name FROM employees
WHERE name LIKE '%ar%';
This returns all employees whose name contains 'ar' anywhere.
Example 5: Names where second letter is 'a'
SELECT name FROM employees
WHERE name LIKE '_a%';
This returns all employees whose second letter is 'a'.
Tip
LIKEis case-insensitive in some databases (like MySQL) but case-sensitive in others (like PostgreSQL unless usingILIKE).- You can also combine
LIKEwithNOTto exclude patterns.