EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials SQL Tutorial SQL Wildcard Character
 

SQL Wildcard Character

Updated March 14, 2023

SQL Wildcard Character

 

 

Introduction to SQL Wildcard Character

SQL wildcard character are used for specifying the pattern that should be followed by the value to be compared. We can match the string and check for its matching with different patterns using the LIKE operator in SQL, which is a logical operator that compares the string and searches for the part that satisfies and matches the pattern that is specified using a collection of various regular and wildcard characters. In this article, we will see about the matching of the values or contents of columns and variables using the LIKE operator and wildcard characters for pattern specification, its syntax, some of the wildcard characters and specification method and some of the examples demonstrating its implementation.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax:

Given below is the syntax of the LIKE operator:

column name or expression LIKE pattern [ESCAPE character to be escaped]

Where column name or expression can be the name of the column of the particular table that you want to match with the pattern that is defined using regular and wildcard characters, we can even provide the character that is to be skipped while matching by specifying them in character to be escaped after the ESCAPE keyword which is again an optional thing. Mentioning the pattern with which we want to match the expression is required and must be specified. The LIKE operator can be used for matching in the query statements, including SELECT, INSERT, UPDATE, and DELETE statements.

Pattern Specification and Wildcard Usage

The pattern we have to match the expression can be a sequence of the regular and wildcard characters. The wildcard characters provide flexibility and variety in matching the expressions.

Regular characters are the string of alphabets and numbers that we want to search for, while wildcard characters can be one of the following:

  • The wildcard with percentile signature (%) is used to specify that there can be one or more character occurrences over this place.
  • The underscore character can be used to specify that there can be an occurrence of any of the single characters at the place where the single underscore wildcard character is specified (_).
  • We can specify the list of the characters that can be allowed for a single occurrence at that place by mentioning them inside the square brackets [comma-separated list of allowed characters].
  • We can even specify the range between which we can allow the single occurrence of the character within a specified range by mentioning the starting and ending character within range inside square brackets [starting character – ending character].
  • Alternatively, we can also allow the presence of a single character that is not within the specified range by mentioning the range to be excluded between square brackets prefixing the range with ^ character [^].

Examples of SQL Wildcard Character

Given below are the examples of SQL Wildcard Character:

Let us consider the example by using a table named dictionary that contains the following records:

Code:

SELECT * FROM dictionary;

Output:

SQL Wildcard Character 1

Now, we have to search for the records that contain the “word” string in their column values of meaning.

For this, we will use the following query containing the LIKE function.

Code:

SELECT * FROM dictionary WHERE meaning LIKE "%word%";

The execution of the above query statement gives the following output containing two records which have “word” in their meaning.

Output:

SQL Wildcard Character 2

Using underscore (_) wildcard character to specify the single occurrence of any character between the specified strings or characters, we will consider one example where we will only get the records from the dictionary table that match the pattern that contains as many strings before and after the occurrence of I and I lying in between which can have any character in between the two I’s and specify _ underscore in between.

Our pattern will be “%i_i%”, and the query statement will be as follows:

Code:

SELECT * FROM `dictionary` WHERE meaning LIKE "%i_i%";

Output:

SQL Wildcard Character 3

The output containing the above records were retrieved because of occurrence of words like “additional”, “origins”, “writing”, “similar” and “originality” in them that had only one character between two I characters and any of the words and characters before and after that pattern as specified by a % wildcard character.

Let us consider one more existing table named suppliers whose contents and structure are as shown in the query statement’s output.

Code:

SELECT * FROM Suppliers;

Output:

one more existing table named suppliers

Now, we want to retrieve all the records of the suppliers whose name starts from the character A, G or T. For this, we will use the wildcard characters of square brackets and specify the allowed characters in the start position and further with the help of percentile wildcard character we will mention that any string or presence of any characters is allowed after the occurrence of one of the characters that are mentioned in our square brackets.

Hence, our pattern will be [AGT]% that includes wildcard characters, and our query statement will be:

Code:

SELECT * FROM Suppliers where SuppliersName like '[AGT]%';

Output:

SQL Wildcard Character 5

Now, let us consider one more example that will demonstrate the usage and specification of the character that lies between the specified range of the alphabets. As discussed above, the range can be specified inside square brackets beginning and ending character of the range and a dash between. We want to retrieve all suppliers whose city names start within the range of D and H characters, followed by any characters.

Code:

SELECT * FROM Suppliers where city like '[D-H]%';

Output:

usage and specification

Conclusion

Many wildcard characters help us to define the patterns that will be used for matching the string values. The strings and column values and expressions can be matched with a particular pattern by using the LIKE operator in SQL that helps us specify the pattern that is the collection of different regular and wildcard characters. The use of wildcard characters makes the matching and pattern specification more flexible and easy.

Recommended Articles

We hope that this EDUCBA information on “SQL Wildcard Character” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. SQL Select Top
  2. SQL Cluster
  3. SQL Except Select
  4. SQL DROP TRIGGER

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW