LIKE Operator in SQL
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.
Table of Content
Best-suited Data Analytics courses for you
Learn Data Analytics with these high-rated online courses
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
- There are two wildcard characters that are very often used with the LIKE operator
Must Read: SQL IN
Must Read: SQL BETWEEN
Syntax:
SELECT column_namesFROM table_namesWHERE column_name LIKE pattern;
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 |
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 EmployeeWHERE Name LIKE βG%β;
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 EmployeeWHERE Name LIKE β%yβ
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 EmployeeWHERE Department LIKE β%in%β;
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 EmployeeWHERE Education LIKE β_G%β
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, EducationFROM EmployeeWHERE Education LIKE βP_%β;
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, EducationFROM EmployeeWHERE Department LIKE βE%gβ;
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, DepartmentFROM EmployeeWHERE Month of Joining NOT LIKE β%r%β;
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, DepartmentFROM EmployeeWHERE Department NOT ILIKE β%r%β;
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.
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