Wildcards in SQL

Learn via video course
FREE
View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Topics Covered

What are Wildcards in SQL?

A wildcard is a character or a group of characters that can be used to replace any other character or characters in a value defined by its constraints. In SQL, the LIKE operator uses wildcard characters. The LIKE operator is used to look for a certain pattern in a column when used with the WHERE clause.

Why Do We Need Wildcards in SQL?

Imagine that you are tasked with finding the salaries of all people whose names start with A. Alternatively, imagine you want to find the salaries of people whose name starts and ends with A. All the data is stored in SQL in the form of tables in a database. Such a task would be difficult using simple SELECT queries. That's where wildcards enter. SQL Wildcards are used to search for complex data and are handy in speeding up the querying process.

How Many Wildcards are There in SQL?

In SQL, wildcards are used with the LIKE operator. There are 4 wildcards in SQL. They are as follows:

WildcardDescription
%Zero or more characters
_Single Character
[list_of_char]Range of characters to match
[!list_of_char]Sets the range of characters to not be matched
[^list_of_char]Sets the range of characters to not be matched

Examples of Wildcards in SQL

To understand the concept of wildcards in detail, we will consider a table DETAILS that will be used for the examples below.

NameLocationColour
AnnaPeruRed
AdyaChileRed
AparjeetDubaiYellow
BohemaSingaporeGreen
CathyKenyaWhite

1. %Description: The percentage sign (%) is used to match zero or more characters. The characters can be either digits, alphabets or special characters.

Syntax:

Example 1: Display details of those whose names start in A.

Output:

NameLocationColour
AnnaPeruRed
AdyaChileRed
AparjeetDubaiYellow

Example 2: Display details of those whose name end with an A.

Output:

NameLocationColour
AnnaPeruRed
AdyaChileRed
BohemaSingaporeGreen

Example 3: Display details of those whose name contain an A.

Output:

NameLocationColour
AnnaPeruRed
AdyaChileRed
AparjeetDubaiYellow
BohemaSingaporeGreen
CathyKenyaWhite

2. _ Description: The underscore (_) is used to denote a single character. The characters can be either digits, alphabets or special characters.

Syntax:

Example 1: Colour containing 3 characters and ending with 'ed'

Output:

NameLocationColour
AnnaPeruRed
AdyaChileRed

Example 2: Colour containing 5 characters and starting with 'Wh'

Output:

NameLocationColour
CathyKenyaWhite

3. [list_of_char] Description: Matches a character with a character in the list of characters that are surrounded by square brackets.

Syntax:

Example 1: Details of one whose location contains at least one vowel.

Output:

NameLocationColour
AnnaPeruRed
AdyaChileRed
AparjeetDubaiYellow
BohemaSingaporeGreen
CathyKenyaWhite

Example 2: Details of one whose location contains ends in any letter between a to e.

Output:

NameLocationColour
AdyaChileRed
BohemaSingaporeGreen
CathyKenyaWhite

4. [!list_of_char] or [^list_of_char] Description: Matches when the character is not equal to any of the characters mentioned in the list. The characters can be either digits, alphabets or special characters.

Syntax:

Example 1: Details of one whose location does not end in the letter E.

Output:

NameLocationColour
AparjeetDubaiYellow
CathyKenyaWhite

Example 2: Details of one whose location does not start with the letter S.

Output:

NameLocationColour
AnnaPeruRed
AdyaChileRed
AparjeetDubaiYellow
CathyKenyaWhite

Learn more about advanced concepts of SQL

To learn more about some interesting and advanced concepts in SQL, check out our SQL Reading List on Scaler Topics.

Conclusion

  • Wildcards in SQL are used to match a character or a group of characters in SQL and are often used in the WHERE clause in case of complex queries.
  • They reduce computation times and help in query processing
  • Wildcards are used with the LIKE operator in SQL.
  • There are 4 commonly used wildcards which have been explained with examples in the article above.