In this tutorial, we will learn about the Wildcard characters of SQL.
Wildcard characters are used to substitute any other characters in a string.
Wildcard characters are used with the SQL LIKE
operator with the WHERE
clause and can be used to search data of a specified pattern.
The %
(Percent Sign) and _
(Underscore) sign both can also be used in combinations.
In SQL, Wildcards are as follow:
WildCard | Description |
% | A percent sign (%) represents a zero, one or multiple characters. |
Underscore ( _ ) | underscore character ( _ ) represents a single character. |
[charlist] | represents any single character within a charlist. |
[^charlist] or [!charlist] | represents any single character that is not present in the charlist |
Let us consider this table "Employee" for records.
Table Name: Employee
ID | EmpName | City | Country | Gender | Salary |
1 | Shankar | Delhi | India | male | 25000 |
2 | Sourabh | Delhi | India | male | 30000 |
3 | Ranvijay | Mumbai | India | male | 15000 |
4 | Kapil | Noida | India | male | 25000 |
5 | Shalini | Jaipur | India | female | 18000 |
6 | Rakesh | Faridabad | India | male | 23000 |
7 | Akshay | Mumbai | India | male | 21000 |
8 | Sarah | New York | US | female | 76000 |
9 | Rocky | Noida | India | male | 28000 |
Here is an example of LIKE
operator where an employee name starting with "S", aquery will be:
SELECT * FROM Employee
WHERE EmpName LIKE 'S%';
ID | EmpName | City | Country | Gender | Salary |
1 | Shankar | Delhi | India | male | 25000 |
2 | Sourabh | Delhi | India | male | 30000 |
5 | Shalini | Jaipur | India | female | 18000 |
8 | Sarah | New York | US | female | 76000 |
Here is another example of LIKE
operator where an employee name ending with "%h"
. a query will be:
SELECT * FROM Employee
WHERE EmpName LIKE '%h';
ID | EmpName | City | Country | Gender | Salary |
2 | Sourabh | Delhi | India | male | 30000 |
6 | Rakesh | Faridabad | India | male | 23000 |
8 | Sarah | New York | US | female | 76000 |
Here is an another example of LIKE
operator where an Employee Name that have "%ra%" in any position, query will be:
SELECT * FROM Employee
WHERE EmpName LIKE '%ra%';
ID | EmpName | City | Country | Gender | Salary |
2 | Sourabh | Delhi | India | male | 30000 |
3 | Ranvijay | Mumbai | India | male | 15000 |
6 | Rakesh | Faridabad | India | male | 23000 |
8 | Sarah | New York | US | female | 76000 |
Here is an example of LIKE
operator where an employee name that has "a" at a third position, query will be:
SELECT * FROM Employee
WHERE EmpName LIKE '__a%';
ID | EmpName | City | Country | Gender | Salary |
1 | Shankar | Delhi | India | male | 25000 |
5 | Shalini | Jaipur | India | female | 18000 |
Here is an example of LIKE
operator where an employee name that starts with "s" and ends with "h", a query will be:
SELECT * FROM Employee
WHERE EmpName LIKE 's%h';
ID | EmpName | City | Country | Gender | Salary |
2 | Sourabh | Delhi | India | male | 30000 |
8 | Sarah | New York | US | female | 76000 |
Here is an example of LIKE
operator where an employee name that not starts with "s", a query will be:
SELECT * FROM Employee
WHERE EmpName NOT LIKE 's%';
ID | EmpName | City | Country | Gender | Salary |
3 | Ranvijay | Mumbai | India | male | 15000 |
4 | Kapil | Noida | India | male | 25000 |
6 | Rakesh | Faridabad | India | male | 23000 |
7 | Akshay | Mumbai | India | male | 21000 |
9 | Rocky | Noida | India | male | 28000 |
Here is an example of LIKE
operator where an employee name that starts with "r", "a" or "k", a query will be:
SELECT * FROM Employee
WHERE EmpName LIKE '[rak]%';
ID | EmpName | City | Country | Gender | Salary |
3 | Ranvijay | Mumbai | India | male | 15000 |
4 | Kapil | Noida | India | male | 25000 |
6 | Rakesh | Faridabad | India | male | 23000 |
7 | Akshay | Mumbai | India | male | 21000 |
9 | Rocky | Noida | India | male | 28000 |
Here is an example of LIKE
operator where an employee name that not starts with "r", "a" or "k", a query will be:
SELECT * FROM Employee
WHERE EmpName NOT LIKE '[rak]%';
OR you can write like this:
SELECT * FROM Employee
WHERE EmpName LIKE '[!rak]%';
ID | EmpName | City | Country | Gender | Salary |
1 | Shankar | Delhi | India | male | 25000 |
2 | Sourabh | Delhi | India | male | 30000 |
5 | Shalini | Jaipur | India | female | 18000 |
8 | Sarah | New York | US | female | 76000 |