SQL wildcard characters are special symbols used to represent one or more unknown characters in a string. They help in searching and filtering text data based on patterns instead of exact matches. Wildcards are mainly used with the LIKE and NOT LIKE operators to perform flexible searches.
- % (Percent): Matches any number of characters.
- _ (Underscore): Matches one single character.
- LIKE: Finds values that match a pattern.
- NOT LIKE: Finds values that do not match a pattern.
Syntax:
SELECT column1,column2 FROM table_na
WHERE column LIKE wildcard_operator;
- column1,column2: fields in the table
- table_name: name of the table
- column: name of the field used for filtering data
Types of SQL Wildcard Characters
There are several wildcard characters in SQL, each serving a different purpose in pattern matching. Let’s break down the most common wildcard characters and their usage:
- %: Represents zero or more characters.
- _: Represents exactly one character.
- [ ]: Represents a range of characters, used to match any single character within the range.
- [ ] with ^ or !: Matches any character that is NOT in the specified range.
Example of SQL Wildcard Characters
We will use a Customer table to show how wildcard characters work in SQL searches.

1. Using the % Wildcard
The % wildcard is used to substitute for zero or more characters. It’s very flexible and is commonly used for matching partial strings.
Example 1: Records Starting with a Specific Letter
To fetch records where the CustomerName starts with the letter 'A'. This query will return all records where the CustomerName starts with the letter A, regardless of what comes after it.
Query:
SELECT *
FROM Customer
WHERE CustomerName LIKE 'A%';Output:

Example 2: Records Ending with a Specific Letter
To fetch records from the Customer table with NAME ending with the letter 'A'. This query retrieves all customers whose name ends with A, regardless of what comes before it.
Query:
SELECT *
FROM Customer
WHERE CustomerName LIKE '%l';Output

Example 3: Records Containing a Specific Letter at Any Position
To fetch records from the Customer table with NAME with the letter 'A' at any position. This query will return records where A appears anywhere within the name, whether at the beginning, middle, or end.
Query
SELECT * FROM Customer WHERE CustomerName LIKE '%A%';Output:

Example 4: Fetching Records with 'ra' in Country Field and Removing Duplicates
To fetch records from the Customer table where the Country contains the substring 'ra' at any position, and ensure the result set does not contain duplicate data:
Query:
SELECT DISTINCT * FROM Customer WHERE Country LIKE '%ra%';Output:

2. Using the _ Wildcard
The _ wildcard is used to substitute for exactly one character. This is useful when we know part of the string but need to match one specific unknown character.
Example 1: Records with a Specific Prefix and Any Three Letters
This query fetches records where the CustomerName starts with “Nav” and has exactly three more characters after it.
Query:
SELECT * FROM Customer WHERE CustomerName LIKE 'Dan___';Output:

Example 2: Records with a Specific Length
This query fetches customers whose Country name has exactly 7 characters.
Query:
SELECT * FROM Customer WHERE Country LIKE '_______';Output:

3. Using the [ ] Wildcard
The [ ] wildcard allows us to specify a set or range of characters that we want to match. It is especially useful when we need to search for specific characters within a defined range or group.
Example 1: Matching One Character from a Set
This query fetches customers whose LastName starts with A, B, or C.
Query
SELECT * FROM Customer WHERE LastName REGEXP '[A-C]';Output:

Example 2: Matching Characters Outside a Range
To fetch records from the Customer table with LastName not containing letters 'y', or 'z'. This query retrieves customers whose last names do not contain the letters Y or Z.
Query
SELECT * FROM Students WHERE LastName NOT LIKE '%[y-z]%';Output:

4. Combining Both % and _ Wildcard
We can combine % and _ to create more accurate search patterns.% matches many characters and _ matches one, helping filter data more precisely.
Example: Matching Specific Digits in a Phone Number
This query finds records where the phone number starts with 8, has any two digits next, and 5 as the fourth digit.
Query
SELECT * FROM Student WHERE PHONE LIKE '8__5%';Output:


