The Wayback Machine - https://web.archive.org/web/20260307224534/https://www.geeksforgeeks.org/sql/sql-wildcard-operators/

SQL Wildcard Characters

Last Updated : 30 Jan, 2026

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.

Screenshot-2026-01-29-180226

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:

Screenshot-2026-01-17-144641

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

Screenshot-2026-01-17-154128

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:

Screenshot-2026-01-17-154801

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:

Screenshot-2026-01-17-154905

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:

Screenshot-2026-01-17-155105

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:

Screenshot-2026-01-17-155215

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:

Screenshot-2026-01-29-180544

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:

Screenshot-2026-01-29-180723

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:

Screenshot-2026-01-29-180843
Comment