The LIKE operator is used in SQL to search for a specific pattern in a column. The percent symbol (%), also known as a wildcard, represents zero, one, or multiple characters, while the underscore (_) represents a single character. In Microsoft Access, the question mark (?) is used instead of the underscore to represent a single character. This operator can be used in a SELECT, INSERT, UPDATE, or DELETE statement
Example:
Employee Table
| emp_id | emp_name | salary | dept_id | manager_id |
|---|---|---|---|---|
| E1 | Rahul | 15000 | D1 | M1 |
| E2 | Manoj | 15000 | D1 | M1 |
| E3 | James | 55000 | D2 | M2 |
| E4 | Michael | 25000 | D2 | M2 |
| E5 | Ali | 20000 | D10 | M3 |
| E6 | Robin | 35000 | D10 | M3 |
To select all employees with a name that starts with a, you can use the following SQL statement:
SELECT * FROM Employee WHERE emp_name LIKE 'a%';
Output
| emp_id | emp_name | salary | dept_id | manager_id |
|---|---|---|---|---|
| E5 | Ali | 20000 | D10 | M3 |
To select all employees with a name that ends with s, you can use the following SQL statement:
SELECT * FROM Employee WHERE emp_name LIKE '%s';
Output
| emp_id | emp_name | salary | dept_id | manager_id |
|---|---|---|---|---|
| E3 | James | 55000 | D2 | M2 |
To select all employees with a name that contains the letter a in any position, you can use the following SQL statement:
SELECT * FROM Employee WHERE emp_name LIKE '%a%';
Output
| emp_id | emp_name | salary | dept_id | manager_id |
|---|---|---|---|---|
| E1 | Rahul | 15000 | D1 | M1 |
| E2 | Manoj | 15000 | D1 | M1 |
| E3 | James | 55000 | D2 | M2 |
| E4 | Michael | 25000 | D2 | M2 |
| E5 | Ali | 20000 | D10 | M3 |
The SQL statement below chooses all workers whose names begin with a and have at least three characters:
SELECT * FROM Employee WHERE emp_name LIKE 'a__%';
Output
| emp_id | emp_name | salary | dept_id | manager_id |
|---|---|---|---|---|
| E5 | Ali | 20000 | D10 | M3 |
Code Runner
Copy the below queries in the code runner to see the result
SELECT * FROM Employee WHERE emp_name LIKE 'a%';
SELECT * FROM Employee WHERE emp_name LIKE '%s';
SELECT * FROM Employee WHERE emp_name LIKE '%a%';
SELECT * FROM Employee WHERE emp_name LIKE 'a__%';