SQL - LIKE Operator
The LIKE operator is used in the WHERE condition to filter data based on some specific pattern. It can be used with numbers, string, or date values. However, it is recommended to use the string values.
The LIKE operator in MS SQL Server, SQLite, MySQL database are not case-sensitive, whereas it is case-sensitive in Oracle, and PostgreSQL database.
Syntax:
SELECT * FROM table_name
WHERE column_name LIKE 'pattern'
The LIKE operator uses the following wildcard characters to specify a pattern:
Pattern | Description |
---|---|
% | The % matches zero, one, or multiple characters (capital or small) or numbers. E.g. 'A%' will matche all string starting with 'A' and followed by any number of characters or numbers. |
_ |
The underscore _ sign matches any single character or number. E.g. 'A_' will match all strings with two chars where the first character must be 'A' and second character can be anything. |
[] |
The [] matches any single character within the specified range in the []. E.g. 'A[e,l,p]' will match 'Apple', 'Aelp', 'Alep', 'Aple', etc. |
[^] | The [^] matches any single character except the specified range in the [^]. E.g. 'A[^e,l,p]' will match anything that starts with 'A', but not 'Apple', 'Aelp', 'Alep', 'Aple', etc. |
These wildcard characters can be used individually or in combination with each other in the LIKE operator.
For the demo purpose, we will use the following Employee
tables in all examples.
EmpId | FirstName | LastName | Salary | HireDate | |
---|---|---|---|---|---|
1 | 'John' | 'King' | '[email protected]' | 33000 | 2018-07-25 |
2 | 'James' | 'Bond' | 2018-07-29 | ||
3 | 'Neena' | 'Kochhar' | '[email protected]' | 17000 | 2018-08-22 |
4 | 'Lex' | 'De Haan' | '[email protected]' | 15000 | 2018-09-8 |
5 | 'Amit' | 'Patel' | 18000 | 2019-01-25 | |
6 | 'Abdul' | 'Kalam' | '[email protected]' | 25000 | 2020-07-14 |
Consider the following query with the LIKE operator.
SELECT *
FROM Employee
WHERE FirstName LIKE 'john';
Above, WHERE FirstName LIKE 'john'
retrieves all the records where the value in the FirstName
column is 'john' or 'John' in MS SQL Server, SQLite, and MySQL database. However, the LIKE operator is case-sensitive in Oracle and PostgreSQL database, so it only fetches records where the value is 'john', not 'John'.
The following is the result in MS SQL Server, SQLite, and MySQL database.
EmpId | FirstName | LastName | Salary | HireDate | |
---|---|---|---|---|---|
1 | 'John' | 'King' | '[email protected]' | 33000 | 2018-07-25 |
The wildcard char % specifies any number of characters.
SELECT *
FROM Employees
WHERE FirstName LIKE 'j%';
The above query fetches all the records where the value of the FirstName
column starts with either 'j' or 'J' followed by any number of characters in MS SQL Server, SQLite, and MySQL database. In Oracle or PostgreSQL, it will fetch records starts with 'j' but not 'J'. The following is the result in MS SQL Server, SQLite, and MySQL database.
EmpId | FirstName | LastName | Salary | HireDate | |
---|---|---|---|---|---|
1 | 'John' | 'King' | '[email protected]' | 33000 | 2018-07-25 |
2 | 'James' | 'Bond' | 2018-07-29 |
The following query retrieves data where FirstName
value is '%a%'. It means the value must contain 'a' in any position.
SELECT *
FROM Employee
WHERE FirstName LIKE '%a%';
The above query will display the following result.
EmpId | FirstName | LastName | Salary | HireDate | |
---|---|---|---|---|---|
2 | 'James' | 'Bond' | 2018-07-29 | ||
3 | 'Neena' | 'Kochhar' | '[email protected]' | 17000 | 2018-08-22 |
5 | 'Amit' | 'Patel' | 18000 | 2019-01-25 | |
6 | 'Abdul' | 'Kalam' | '[email protected]' | 25000 | 2020-07-14 |
The bellow query will return records whose FirstName
value contains three letters and 'e' at the second position.
The '_' indicates any one character.
SELECT *
FROM Employee
WHERE FirstName LIKE '_e_';
EmpId | FirstName | LastName | Salary | HireDate | |
---|---|---|---|---|---|
4 | 'Lex' | 'De Haan' | '[email protected]' | 15000 | 2018-09-8 |
The bellow query uses the [] wildcard pattern.
SELECT *
FROM Employee
WHERE FirstName LIKE 'A[i,m,t,y,s]';
EmpId | FirstName | LastName | Salary | HireDate | |
---|---|---|---|---|---|
5 | 'Amit' | 'Patel' | 18000 | 2019-01-25 |
The bellow query uses the [^] wildcard pattern.
SELECT *
FROM Employee
WHERE FirstName LIKE 'A[^i,m,t,y,s]';
EmpId | FirstName | LastName | Salary | HireDate | |
---|---|---|---|---|---|
6 | 'Abdul' | 'Kalam' | '[email protected]' | 25000 | 2020-07-14 |
The following table list comman patterns used with the LIKE operator:
Pattern | Description |
---|---|
FirstName LIKE 'john' | Returns records whose FirstName value is 'john' or 'JOHN' or 'John' or 'jOhN' or 'JoHn'. |
FirstName LIKE 'j%' | Returns records whose FirstName value starts with 'j' or 'J' followed by any number of characters or numbers. |
FirstName LIKE '%a%' | Returns records whose FirstName value contains 'a' or 'A' at any position. |
FirstName LIKE 'a%b' | Returns records whose FirstName value should start with 'a' or 'A' and last character should be 'b' or 'B'. |
FirstName LIKE '_a' | Returns records whose FirstName value contains two characters and the second character must be 'a' or 'A'. |
FirstName LIKE '_a%' | Returns records whose FirstName value contains the second characters 'a' or 'A'. |
FirstName LIKE '%a_' | Returns records whose FirstName value has the second last character is either 'a' or 'A'. |
FirstName LIKE '___' | Returns records whose FirstName value must be three characters long. |
FirstName LIKE '___%' | Returns records whose FirstName value must contain at least three characters or more. |
FirstName LIKE 'A[i,m,t]' | Returns records whose FirstName value starts from 'A' and followed by any characters specified in []. |
FirstName LIKE 'A[^i,m,t]' | Returns records whose FirstName value starts from 'A' and should not followed by any characters specified in []. |
NOT LIKE
Use the NOT operator with the LIKE operator to filter records that do not match with the specified string.
SELECT *
FROM Employee
WHERE FirstName NOT LIKE 'j%';
Above, FirstName NOT LIKE 'j%'
retrieves records where the FirstName
values not start with 'j'.
EmpId | FirstName | LastName | Salary | HireDate | |
---|---|---|---|---|---|
3 | 'Neena' | 'Kochhar' | '[email protected]' | 17000 | 2018-08-22 |
4 | 'Lex' | 'De Haan' | '[email protected]' | 15000 | 2018-09-8 |
5 | 'Amit' | 'Patel' | 18000 | 2019-01-25 | |
6 | 'Abdul' | 'Kalam' | '[email protected]' | 25000 | 2020-07-14 |