SQL Wildcard Characters With Example

FREE Online Courses: Click for Success, Learn for Free - Start Now!

In this tutorial, we will focus on the SQL wildcards and discuss it in detail. We will study each available wildcard in detail and then discuss the examples to clear our concepts.

What are Wildcards in SQL?

SQL wildcards are put to substitute one or more characters in a string. Wildcards come in handy when we need to compare the strings and also aim to get the minute details.

Wildcards are put up with the LIKE operator and come in handy for solving complex queries.

For example, let us imagine a condition where we have a large amount of data available. And we want to find out details of all the people who lie in the age group 20 to 30 whose names start from A or N.

For resolving such complex queries we use wildcards.

Some of the most commonly used wildcards are as follows:

1. Wildcards in MS Access

Sr.NoRepresentation SymbolDescription
1*Stands for zero or more characters.
2?Stands for a single character.
3[ ]Stands for a single character within the brackets.
4!Stands for the characters not available in the bracket.
5Stands for a range of characters.
6#Stands for a single numeric character.

2. Wildcards in SQL Server

Sr.NoRepresentation SymbolDescription
1%Stands for zero or more characters.
2_Stands for a single character.
3[ ]Stands for a single character specific to the brackets.
4^Stands for any character not in brackets.
5Stands for a range of characters.

Why do we Need Wildcards?

We use SQL wildcards when we need to search for complex data. This complex data could compromise strings or numerical with special characteristics.

Wildcards also come in handy when we want to speed up our querying process. The results are considerably fast when wildcards are in use.

We have already seen SQL Wildcards are put up with the LIKE operator.

Let us consider the usage and then we would discuss the examples:

Sr.NoUsage with LIKE operatorDescription
1WHERE columnName LIKE ‘a%’Returns values which start with ‘a’.
2WHERE columnName LIKE ‘%a’Returns values which end with ‘a’.
3WHERE columnName LIKE ‘%ab%’Returns any value that has ‘ab’ in any position.
4WHERE columnName LIKE ‘_a%’Returns any value which has ‘a’ as the second character.
5WHERE columnName LIKE ‘a_%_%’Returns any value that starts with ‘a’ and is at least 3 characters in length.
6WHERE columnName LIKE ‘a%b’Returns any value which starts with ‘a’ and ends with ‘b’.

Demo Database

Let us understand the database we will be using in this tutorial.
Query: 

SELECT * FROM DataFlair_emp2;

demo database

Here we can see the contents of our demo database.

Example 1: Let us find out the name of employees starting with ‘A’.
Query:

SELECT * FROM dataflair_emp2
WHERE name_emp LIKE 'A%';

SQL wildcard example

Here we can see the names of employees who have ‘A’ as the first character in the name.

Example 2: Let us find the employees whose locations contain ‘nd’.
Query:

SELECT * FROM dataflair_emp2
WHERE location LIKE '%nd%';

Wildcard in SQL

We can observe the locations with the required pattern.

Example 3: Let us find the employee with three characters in his or her name.
Query:

SELECT * FROM dataflair_emp2
WHERE name_emp LIKE '___';

Example of wildcards in SQL

Here we can see the details of employees with three characters in the name field.

Example 4: Let us now find the cities which end with ‘e’ in our database.
Query:

SELECT location as City FROM dataflair_emp2
WHERE location LIKE '%e';

wildcard characters in SQL

Here we can see the details of the required locations according to the filter.

Example 5: Let us find the details of locations that have ‘u’ as the second character.
Query:

 SELECT location as City FROM dataflair_emp2
WHERE location LIKE '_u%';

wildcard characters in SQL

Here we can see the results of locations that have ‘u’ as the second character.

Example 6: Let us find the details of locations with the first character as ‘P’ and the last character as ‘e’.
Query:

SELECT * FROM dataflair_emp2
WHERE location LIKE 'p%e';

Example of wildcard characters in SQL

Here we can see the details of locations starting with ‘p’ and ending with ‘e’.

Summary

In this tutorial, we have discussed and understood how SQL wildcards are put and how they are beneficial. We have discussed various examples.

We have also understood how we can use Wildcards to resolve complex queries.

When we use wildcards, we need to take care to use the LIKE operator as the wildcards are functional only with the LIKE operator.

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

1 Response

  1. Dibyadisha says:

    Nyc it helped me a lot

Leave a Reply

Your email address will not be published. Required fields are marked *