EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials SQL Tutorial SQL LIKE Clause

SQL LIKE Clause

By Priya PedamkarPriya Pedamkar

SQL LIKE Clause

What is the SQL LIKE Clause?

An SQL can have multiple condition statements, and ‘Like’ is one such condition that can be used along with the ‘Where’ condition clause. ‘Like’ clause can be worked with two operators, namely ‘%’ and ‘_’. In some applications instead of ‘_’, ‘?’ is allowed, and instead of ‘%’, ‘*’ is allowed. Here, ‘%’ is used in place for a one or multiple characters, while ‘_’ is used in place for only one character.

In LIKE clause there are two wildcards used in combination they are

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

  • Percent sign (%)
  • Underscore ( _ )

The percent sign symbolizes zero, one or several characters. The underscore symbolizes a distinct number or character. The use of wildcards is to achieve pattern matching in a query. The LIKE condition will be applied in the WHERE clause of INSERT, DELETE, UPDATE or SELECT statement.

Syntax:

The basic syntax of LIKE clause % and _ is as follows

SELECT * FROM table_name WHERE column LIKE 'X%'
SELECT * FROM table_name WHERE column LIKE '%X%'
SELECT * FROM table_name WHERE column LIKE 'X_'
SELECT * FROM table_name WHERE column LIKE '_X'
SELECT * FROM table_name WHERE column LIKE '_X_'

By using AND or OR operators we can combine a number of conditions, here X be any string or a numeric value.

How LIKE Clause works in SQL?

The LIKE Clause is a logical operator that concludes a character string matches with a specified pattern. Regular and wildcard characters were included in pattern matching. In SQL Server LIKE operator specifically used in the WHERE clause includes SELECT, DELETE and UPDATE statements, to retrieve data based on row-wise pattern matching.

Examples

1. SQL SERVER LIKE

Let’s see the sample database of Customer_Master,

Customer Master

2. (Percent) % Wildcard

The percent wildcard defines the string of zero or more characters. Let’s see the examples

By using the percent wildcard to find out or retrieve the customers whose first name starts with the letter Z,

Select Customer_Id, Customer_First Name,customer_Last Name
From Customer_Master
Where Customer_First Name Like 'z%'
Order by Customer_First Name;

Percent

By using the percent wildcard to find out or retrieve the customers whose last name ends with the letterer,

Select Customer_Id, Customer_First Name,customer_Last Name
From Customer_Master
Where Customer_Last Name Like '%er'
Order by Customer_First Name;

Customer Lastname

To find out or retrieve the customers whose last name starts with the letter t and ends with letter s,

Select Customer_Id, Customer_First Name,customer_Last Name
From Customer_Master
Where Customer_Last Name Like 't%s'
Order by Customer_First Name;

SQL LIKE Clause-1.4

3. (Underscore) _ Wildcard

The underscore defines a single character. Let’s see the examples below,

By using the underscore wildcard to retrieve the customers where the second character is the letter u, the pattern _u% explains that the first underscore character match with a single character, the second letter u match exactly with the letter u and the third character % symbolize the sequence of characters.

Select Customer_Id, Customer_First Name,customer_Last Name
From Customer_Master
Where Customer_Last Name Like '_U%'
Order by Customer_First Name;

Customer Firstname

4. [List of Characters] Wildcard

The List of characters specifies that a single character within the particular set, the square brackets with the list of characters, for example [ABC] specifies a character in which one of the characters that occur in the list. Let’s see the example, the statement returns the customers where the customer’s last name starting letter starts with either Y or Z,

Select Customer_Id, Customer_First Name,customer_Last Name
From Customer_Master
Where Customer_Last Name Like '[YZ]%'
Order by Customer_Last Name;

SQL LIKE Clause-1.6

5. [Character-Character] Wildcard

The character-to-character wildcard specifies a character within the specified range. It retrieves the character range only specified within the square brackets, for example [A-C] which displays a character specified within range.

Let’s see the example, the statement returns the customers where the customer’s last name starting letter starts with the range A through C,

Select Customer_Id, Customer_First Name,customer_Last Name
From Customer_Master
Where Customer_Last Name Like '[A-C]%'
Order by Customer_First Name;

SQL LIKE Clause-1.7

6. [^Character List or Range] Wildcard

The character list or range in the square bracket with ( ^ ) caret sign specifies a character which not within the character list or range. Let’s see the example, the statement returns the customers record where customer’s last name starting letter should not the letter in the range of A through X,

Select Customer_Id, Customer_First Name,customer_Last Name
From Customer_Master
Where Customer_Last Name Like '[^A-X]%'
Order by Customer_Last Name;

SQL LIKE Clause-1.8 

7. Not Like Operator

The NOT LIKE operator returns the result of which not the character, for example, to retrieve the customers’ record where the first name of the customer should not starts with the letter A,

Select Customer_Id, Customer_First Name,customer_Last Name
From Customer_Master
Where Customer_First Name Not Like 'A%'
Order by Customer_First Name;

SQL LIKE Clause-1.9

Advantages and Characteristics

  • The LIKE clause is useful to retrieve a record to match with a particular pattern.
  • Use wildcard character ‘%’ to know the entire value.
  • Percent wildcard (%) retrieves the string of zero or more than characters.
  • The underscore (_) wildcard retrieves any number of single characters.
  • The [list of characters] wildcard retrieves within a specified set of any single character.
  • The [character-character] retrieves within a specified range of any single character
  • The [^Character List or Range] wildcard retrieves a character which does not within the character-list or within range.

Condition

  • The SQL LIKE condition lets you apply wildcards to achieve pattern matching statements. The LIKE condition applied in the WHERE clause of INSERT, DELETE, UPDATE or SELECT statements.
  • The LIKE condition returns the result as TRUE where the column matches the particular pattern.
  • To reverse the result of the LIKE Condition, use the NOT operator.

Recommended Articles

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

  1. Table in SQL
  2. Types of Joins in SQL
  3. Distinct Keyword in SQL
  4. Database in SQL​ 
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
1000+ Hours of HD Videos
43 Learning Paths
250+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
1500+ Hour of HD Videos
80 Learning Paths
360+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
3000+ Hours of HD Videos
149 Learning Paths
600+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
All in One Data Science Bundle1500+ Hour of HD Videos | 80 Learning Paths | 360+ Courses | Verifiable Certificate of Completion | Lifetime Access
Financial Analyst Masters Training Program1000+ Hours of HD Videos | 43 Learning Paths | 250+ Courses | Verifiable Certificate of Completion | Lifetime Access
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - 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

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
Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*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?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more