LIKE Operator in SQL

LIKE Operator in SQL

4 mins read632 Views Comment
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Nov 22, 2022 19:34 IST

Logical Operators in SQL are used to connect two or more expressions. In this article, we will discuss one of the logical operators i.e. LIKE operator in detail about how and where to use them.

2022_07_SQL-OPERATORS-1.jpg

Table of Content

Recommended online courses

Best-suited Data Analytics courses for you

Learn Data Analytics with these high-rated online courses

β‚Ή2.25 L
3 years
β‚Ή2.25 L
3 years
β‚Ή1.18 L
12 months
β‚Ή35 K
3 months
β‚Ή2.6 L
12 months
β‚Ή97 K
4 months

What is the LIKE Operator?

LIKE operators in SQL are logical operators that allow determining whether a specific character string matches specific patterns. LIKE operator is case-sensitive.

It includes regular and wildcard characters during pattern matching:

  • Regular character must match the character specified in the character string
  • Wildcard can be matched with an arbitrary segment of the character string
    • There are two wildcard characters that are very often used with the LIKE operator
      • Percentage (%) – allows to match any string of any length (including zero characters)
      • Underscore (_)  – allows to match on a single character

Must Read: SQL IN

Must Read: SQL BETWEEN

Syntax:

 
SELECT column_names
FROM table_names
WHERE column_name LIKE pattern;
Copy code

Must Read: Introduction to SQL

Must Read: SQL ACID Properties

Wildcard Characters used with LIKE

Wildcard Character  Description
LIKE β€˜n%’ Find the values that start with n
LIKE β€˜%g’ Find the values that end with g
LIKE β€˜%learn%’ Find the value that has to learn in any position
LIKE β€˜_n%’ Find the letter that has n at the second position
LIKE β€˜n_%’ Find the word that starts with n but has at least two characters in length
LIKE β€˜n%g’ Find the word that starts with n and ends with g

Now, let’s take some example to get a better understand of how to use LIKE operator in SQL.

Here, we will take the Employee Dataset (that contains Employee ID, Name, Gender, Department, Education, Month of Joining, and CTC) to do some examples to get a clear understanding of different logical operators.

Employee ID Name Gender Department Education Month of Joining CTC(in Lacs)
1001 Ajay M Engineering Doctoral January 25
1002 Babloo M Engineering UG February 23
1003 Chhavi F HR P March 15
1004 Dheeraj M HR UG January 12
1005 Evina F Marketing UG March 16
1006 Fredy M Sales UG December 10
1007 Garima F Sales PG March 10
1008 Hans M Admin PG November 8
1009 Ivanka F Admin Intermediate June 7
1010 Jai M Peon December 4
Employee

Must Read: What is the difference between SQL and MySQL?

Must Read: Difference between SQL and NoSQL

Example 1: Find the detail of employee whose name starts with G.

Query

 
SELECT *
FROM Employee
WHERE Name LIKE β€˜G%’;
Copy code

Output

Employee ID Name Gender Department Education Month of Joining CTC(in Lacs)
1007 Garima F Sales PG March 10

Must Read: Subqueries in SQL

Must Read: SQL CREATE TABLE

Example 2: Find the detail of employee whose name ends with y.

Query

 
SELECT *
FROM Employee
WHERE Name LIKE β€˜%y’
Copy code

Output

Employee ID Name Gender Department Education Month of Joining CTC(in Lacs)
1001 Ajay M Engineering Doctoral January 25
1006 Fredy M Sales UG December 10

Must Read: SQL WHERE

Must Read: Introduction to Normalization

Example 3: Find the details of the employee that contains the word β€œin” at any position in the department.

Query

 
SELECT *
FROM Employee
WHERE Department LIKE β€˜%in%’;
Copy code

Output

Employee ID Name Gender Department Education Month of Joining CTC(in Lacs)
1001 Ajay M Engineering Doctoral January 25
1002 Babloo M Engineering UG February 23
1005 Evina F Marketing UG March 16
1008 Hans M Admin PG November 8
1009 Ivanka F Admin Intermediate June 7

Must Read: SQL LIMITS

Must Check: SQL Online Course and Certifications

Example 4: Find the employee detail that contains the word β€˜G’ at the second position in Education.

 
SELECT *
FROM Employee
WHERE Education LIKE β€˜_G%’
Copy code

Output

Employee ID Name Gender Department Education Month of Joining CTC(in Lacs)
1002 Babloo M Engineering UG February 23
1003 Chhavi F HR P March 15
1004 Dheeraj M HR UG January 12
1005 Evina F Marketing UG March 16
1006 Fredy M Sales UG December 10
1007 Garima F Sales PG March 10
1008 Hans M Admin PG November 8

Example 5: Find the name and education of employees for which the education starts with P and has at least two characters.

Query

 
SELECT Name, Education
FROM Employee
WHERE Education LIKE β€˜P_%’;
Copy code

Output

Name Education
Garima PG
Hans PG

Example 6: Find the employee name, department, and education of the employee who is working in the department that starts with β€˜E’ and ends with β€˜g’.

Query

 
SELECT Name, Gender, Education
FROM Employee
WHERE Department LIKE β€˜E%g’;
Copy code

Output

Name Gender Education
Ajay M Doctoral
Babloo M UG

Example 7: Find the employee id, name, and department where the month of joining does not contain the word β€˜r’.

Query

 
SELECT EmployeeID, Name, Department
FROM Employee
WHERE Month of Joining NOT LIKE β€˜%r%’;
Copy code

Output

EmployeeID Name Department
1009 Ivanka Admin

Must Read: SQL Comparison and Arithmetic Operator

Wildcard characters and ILIKE

ILIKE work in a similar fashion as LIKE but it has one additional feature that it is not case-sensitive i.e. if you want to select all the department (in the above employee dataset) that does not contain the word r and R, you can simply use ILIKE instead of LIKE.

Query

 
SELECT EmployeeID, Name, Department
FROM Employee
WHERE Department NOT ILIKE β€˜%r%’;
Copy code

Output

EmployeeID Name Department
1006 Fredy Sales
1007 Garima Sales
1008 Hans Admin
1009 Ivanka Admin
1010 Jai Peon

Conclusion

In this article, we have discussed how to use Like and ILIKE operator in SQL with the help of examples.

Hope this article, will help you in your data science/data analysis journey.

About the Author
author-image
Vikram Singh
Assistant Manager - Content

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio