×

How to use LIKE in SQL

In this SQL article, we will learn and understand how to use LIKE to the columns in the SQL tables.

What is Like?

Like is an operator in the SQL. It is used to find a particular pattern matching records in each row of the field. The Like operator is always used with the WHERE clause in the SQL queries. We can use the Like operator with the SELECT query, the UPDATE query and the DELETE query.

The Syntax for using the Like operator in Structured Query Language:

SELECT columnname1, columnname2, columnname3 FROM tablename WHERE columname LIKE pattern;

In the Structured Query Language, the LIKE operator is used with the following two wildcard characters:

1 Percent Sign (%)

2 Underscore Sign (_)

1 Percent Sign (%):

This character is used to match zero, one, or more than one character in the column.

Syntax of using percent sign with the LIKE operator:

1 The following syntax matches all the values whether its string value or numeric which starts with the character or numeric:

SELECT columnname1, columnname2, columnname3 FROM tablename WHERE columnaname LIKE ‘character%’;

2 The following syntax matches all the values whether its string value or numeric which ends with the character or numeric:

SELECT columnname1, columnname2, columnname3 FROM tablename WHERE columnaname LIKE ‘%character’;

3 The following syntax matches all the values whether its string value or numeric which starts with the character or numeric and ends with characters or numeric:

SELECT columnname1, columnname2, columnname3 FROM tablename WHERE columnaname LIKE ‘starting_character% ending_character’;

4 The following syntax matches all the values whether its string value or numeric which contains the character or numeric at any position:

SELECT columnname1, columnname2, columnname3 FROM tablename WHERE columnaname LIKE ‘%character%’;

2 Underscore Sign (_):

This character is used to match one or a single character.

Syntax of using underscores sign with the LIKE operator:

1 The following syntax matches all the strings or numeric which contains 5 characters and starts with any character:

SELECT columnname1, columnname2, columnname3 FROM tablename WHERE columnaname LIKE ‘character____%’;

2 The following syntax matches all the strings or numeric which contains characters at 3 positions:

SELECT columnname1, columnname2, columnname3 FROM tablename WHERE columnaname LIKE ‘__character%’;

To use SQL Like operator to the column in the table, we have to follow the following steps in the sequence:

Step 1: Create a database or use an existing database.

Step 2: Create the table in the database and insert the values into the table or use the existing table from the selected database.

Step 3: View the inserted records from the table.

Step 4: Now we can use the SQL Like operator to the column in the table.

Now, we will understand the SQL operator with the help of examples.

Step 1: Create a database or use an existing database:

We have already created database name Company:

To use a database we should fire USE query followed by the database name.

Step 2: Create the table and insert the records into the newly created table or use an existing created table. We have created table named Employee.

Step 3: View the records from the table, to view we use the SELECT query followed by table name.

Table: Employee:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65500PUNEORACLE1
1002VAIBHAVSHARMA60000NOIDAC#5
1003NIKHILVANI50500JAIPURFMW2
2001PRACHISHARMA55500CHANDIGARHORACLE1
2002BHAVESHJAIN65500PUNEFMW2
2003RUCHIKAJAIN50000MUMBAIC#5
3001PRANOTISHENDE55500PUNEJAVA3
3002ANUJAWANRE50500JAIPURFMW2
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4001RAJESHGOUD60500MUMBAITESTING4
4002ASHWINIBAGHAT54500NOIDAJAVA3
4003RUCHIKAAGARWAL60000DELHIORACLE1
5001ARCHITSHARMA55500DELHITESTING4

Step 4: Now we can use the SQL Like operator.

Let’s understand the examples with percent sign:

Example 1: Write a query to display the employee id, first name and city from employee table whose employee first name starts with ‘A’.

SELECT employeeid, first_name, city FROM employee WHERE first_name LIKE 'A%';

From the above query, we displayed the employee id, first name and the city name from employee table of that employee whose first name starts with the character A in the table.

Output:

EMPLOYEEIDFIRST_NAMECITY
3002ANUJAJAIPUR
4002ASHWININOIDA
5001ARCHITDELHI
How To Use LIKE In SQL

As shown in the above output, the result contains only those employees whose name starts with ‘A’.

Example 2: Write a query to display the employee id, first name, last name and the city from employee table whose employee city name ends with ‘I’.

SELECT employeeid, first_name, last_name, city FROM employee WHERE city LIKE '%I';

From the above query, we displayed the employee id, first name, last name and the city name from employee table of that employee whose city name ends with the character ‘I’ in the table.

Output:

EMPLOYEEIDFIRST_NAMELAST_NAMECITY
2003RUCHIKAJAINMUMBAI
3003DEEPAMJAUHARIMUMBAI
4001RAJESHGOUDMUMBAI
4003RUCHIKAAGARWALDELHI
5001ARCHITSHARMADELHI
How To Use LIKE In SQL

As shown in the above output, the result contains only those employees whose city name ends with ‘I’.

Example 3: Write a query to display the employee id, first name, last name and the department from employee table whose employee first name starts with ‘P’ and ends with ‘I’.

SELECT employeeid, first_name, last_name, department FROM employee WHERE first_name LIKE 'P%I';

From the above query, we displayed the employee id, first name, last name and the department name from employee table of that employee whose first name starts with the character ‘P’ and ends with character ‘I’ in the table.

Output:

EMPLOYEEIDFIRST_NAMELAST_NAMEDEPARTMENT
2001PRACHISHARMAORACLE
3001PRANOTISHENDEJAVA
How To Use LIKE In SQL

As shown in the above output, the result contains only those employees whose first name ends with ‘P’ and ends with ‘I’ in the table.

Example 4: Write a query to display the employee id, first name, last name, salary and the department from employee table whose employee salary contains ‘5’ at any index position 

SELECT employeeid, first_name, last_name,salary, department FROM employee WHERE salary LIKE '%5%';

From the above query, we displayed the employee id, first name, last name, salary and the department name from employee table of that employee whose salary contains ‘5’ number at any index position between starting index and ending index in the table.

Output:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYDEPARTMENT
1001VAIBHAVIMISHRA65500ORACLE
1003NIKHILVANI50500FMW
2001PRACHISHARMA55500ORACLE
2002BHAVESHJAIN65500FMW
2003RUCHIKAJAIN50000C#
3001PRANOTISHENDE55500JAVA
3002ANUJAWANRE50500FMW
3003DEEPAMJAUHARI58500JAVA
4001RAJESHGOUD60500TESTING
4002ASHWINIBAGHAT54500JAVA
5001ARCHITSHARMA55500TESTING
How To Use LIKE In SQL

As show in the output, the result contains only that employee whose salary includes 5 at any index position like at 2nd position, 3rd position or 4th position.

Let’s understand the examples with underscore sign:

Consider the employee table with following records:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65500PUNEORACLE1
1002VAIBHAVSHARMA60000NOIDAC#5
1003NIKHILVANI50500JAIPURFMW2
2001PRACHISHARMA55500CHANDIGARHORACLE1
2002BHAVESHJAIN65500PUNEFMW2
2003RUCHIKAJAIN50000MUMBAIC#5
3001PRANOTISHENDE55500PUNEJAVA3
3002ANUJAWANRE50500JAIPURFMW2
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4001RAJESHGOUD60500MUMBAITESTING4
4002ASHWINIBAGHAT54500NOIDAJAVA3
4003RUCHIKAAGARWAL60000DELHIORACLE1
5001ARCHITSHARMA55500DELHITESTING4

Example 1: Write a query to display the entire employee table whose employee salary starts with number ‘5’ and contains only five numbers.

SELECT * FROM employee WHERE salary LIKE '5____';

From the above query, we display the entire table of employee where employee salary starts with number 5 and contains only five number salaries in the table.

Output:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1003NIKHILVANI50500JAIPURFMW2
2001PRACHISHARMA55500CHANDIGARHORACLE1
2003RUCHIKAJAIN50000MUMBAIC#5
3001PRANOTISHENDE55500PUNEJAVA3
3002ANUJAWANRE50500JAIPURFMW2
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4002ASHWINIBAGHAT54500NOIDAJAVA3
5001ARCHITSHARMA55500DELHITESTING4
How To Use LIKE In SQL

As show in the output, the result contains only those employees’ records whose salary starts with numeric 5 and contains only 5 number salaries.

Example 2: Write a query to display the entire employee table whose employee city contains U at the second position.

SELECT * FROM employee WHERE city LIKE '_U%';

From the above query, we display the entire table of employee where employees’ city contains U at the second position in the table.

Output:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65500PUNEORACLE1
2002BHAVESHJAIN65500PUNEFMW2
2003RUCHIKAJAIN50000MUMBAIC#5
3001PRANOTISHENDE55500PUNEJAVA3
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4001RAJESHGOUD60500MUMBAITESTING4
How To Use LIKE In SQL

As show in the output, the result contains only those employees’ records whose city contains U at the second position.

Example 3: Write a query to display the entire employee table whose employee department contains J at the first position and V at the third position.

SELECT * FROM employee WHERE Department LIKE 'J_V%';

From the above query, we display the entire table of employee where employees’ department contains J at the first position and V at the third position in the table.

Output:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
3001PRANOTISHENDE55500PUNEJAVA3
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4002ASHWINIBAGHAT54500NOIDAJAVA3
How To Use LIKE In SQL

As show in the output, the result contains only those employees’ records whose department contains J at the first position and V at the third position.

Example 4: Write a query to display the entire employee table whose employee salary contains 5 at the third position.

SELECT * FROM employee WHERE salary LIKE '__5%';

From the above query, we display the entire table of employee where employees’ salary contains 5 at the third position in the table.

Output:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65500PUNEORACLE1
1003NIKHILVANI50500JAIPURFMW2
2001PRACHISHARMA55500CHANDIGARHORACLE1
2002BHAVESHJAIN65500PUNEFMW2
3001PRANOTISHENDE55500PUNEJAVA3
3002ANUJAWANRE50500JAIPURFMW2
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4001RAJESHGOUD60500MUMBAITESTING4
4002ASHWINIBAGHAT54500NOIDAJAVA3
5001ARCHITSHARMA55500DELHITESTING4
How To Use LIKE In SQL

As show in the output, the result contains only those employees’ records whose salary contains 5 at the third position.

We can use multiple LIKE operator in the single SQL query using AND operator and OR operator.

Example 1: Write a query to display the entire employee table whose employee salary contains ‘5’ at the third position or city name ends with ‘I’.

SELECT * FROM employee WHERE salary LIKE '__5%' OR city LIKE '%I';

From the above query, we display the entire table of employee where employees’ salary contains 5 at the third position or city name ends with the character ‘I’ in the table. Here we have used multiple LIKE operator using OR operator between this LIKE operator.

Output:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65500PUNEORACLE1
1003NIKHILVANI50500JAIPURFMW2
2001PRACHISHARMA55500CHANDIGARHORACLE1
2002BHAVESHJAIN65500PUNEFMW2
2003RUCHIKAJAIN50000MUMBAIC#5
3001PRANOTISHENDE55500PUNEJAVA3
3002ANUJAWANRE50500JAIPURFMW2
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4001RAJESHGOUD60500MUMBAITESTING4
4002ASHWINIBAGHAT54500NOIDAJAVA3
4003RUCHIKAAGARWAL60000DELHIORACLE1
5001ARCHITSHARMA55500DELHITESTING4
How To Use LIKE In SQL

As show in the output, the result contains only those employees’ records whose salary contains 5 at the third position or city name ends with the character ‘I’.

Example 2: Write a query to display the entire employee table whose employee first name starts with ‘P’ and department ends with ‘E’.

SELECT * FROM employee WHERE first_name LIKE 'P%' AND department LIKE '%E';

From the above query, we display the entire table of employee where employees’ first name starts with ‘P’ AND department name ends with the character ‘E’ in the table. Here we have used multiple LIKE operator using AND operator between this LIKE operator.

Output:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
2001PRACHISHARMA55500CHANDIGARHORACLE1
How To Use LIKE In SQL

As show in the output, the result contains only those employees’ records whose first name starts with ‘P’ AND department name ends with the character ‘E’.


Related Topics

SQL VIEW

The SQL VIEW concept helps to hide the difficulty of the records and provides limitations to access to the database. The SQL view is similar to the SQL tables. In SQL...

7 minutes read.

SQL Data Control Language

Data Control Language decides to whom should (which user) permit access privileges. GRANT and REVOKE are the commands of DCL. GRANT: It gives privileges to user. REVOKE: It takes back privileges from granted...

1 minute read.

SQL CASE

This page contains all the information about SQL CASE. The CASE is an If-Else type of logical query used in the statement. The CASE in Structured Query Language is similar...

5 minutes read.

SQL INSERT INTO Values

This article will help you in getting a better understanding of a very important SQL INSERT INTO VALUES function. INSERT INTO statement is used to insert or add a new record...

4 minutes read.

WHERE Clause vs HAVING Clause

The WHERE Clause and HAVING clause filter the records in the Structured Query Language queries. The main difference between the WHERE clause and the HAVING clause is the WHERE clause...

5 minutes read.

SQL SELECT BETWEEN Operator

This SQL tutorial explains and helps us understand how to use the BETWEEN operator in the SELECT query with examples. The SQL SELECT BETWEEN operator retrieves the data from the table...

2 minutes read.

SQL Aggregate Operators

In SQL, the aggregate operators perform a calculation on multiple rows or multiple values of a single column and give the output a single value. Here, multiple rows of data are...

4 minutes read.

SQL SELECT OR Operator

This SQL tutorial explains and helps us understand how to use the OR operator in the SELECT query with examples. The OR operator in the Structured Query Language is used to...

3 minutes read.

SQL LOGICAL Operator

In this tutorial, we will understand the operator who falls under the logical operator in SQL with the help of examples. The SQL Logical Operator displays the query result in one...

5 minutes read.

SQL Tutorial for Beginners

SQL tutorial provides basic and advanced concepts of Structured Query Language and how you deploy SQL to work with a relational database system. Our SQL tutorial is designed for beginners...

3 minutes read.

Where vs Having

Difference Between Where and Having The WHERE and HAVING clauses in SQL are used to filter records stored in tables in the databases. The dissimilarities between the WHERE and HAVING clause...

3 minutes read.

SQL COPY Table

In this tutorial, we will learn how to copy tables in SQL with the help of examples. Using the SELECT INTO statement in the SQL we can copy one table into...

4 minutes read.

SQL Aggregate Functions

In this tutorial, we will learn and understand the SQL Aggregate Functions concept with the help of examples. SQL Aggregate Function is a function used to perform calculation operations on one...

4 minutes read.

How to delete column in table

Introduction In SQL, sometimes it is required to delete a column of a table.The use of ALTER TABLE command with DROP COLUMN clause will serve the purpose to delete/remove a column...

4 minutes read.

SQL Top Clause

Top Clause: It returns the first ‘n’ number of rows as output. This Top clause is used to retrieve the required number of rows when there are thousands of records stored...

3 minutes read.

Codd’s Rules in SQL

Codd’s Rules Dr. Edgar F. Codd, in 1985, laid down 13 fundamental rules after doing large-scale research on the Relational Model of databases. According to him, every database must follow these...

3 minutes read.

SQL Arithmetic Operators

This page contains all the information, learn about SQL Arithmetic Operators concept in the SQL table with the help of examples. The Arithmetic Operators is used to perform mathematical calculations on...

5 minutes read.

SQL HAVING Clause

In this tutorial, we will understand the HAVING clause concept in SQL with the help of examples. The HAVING Clause in the SQL is used as we cannot use the WHERE...

3 minutes read.

SQL UNION ALL Operator

This page has all the information about UNION ALL operator, which is used to join all the values from the SELECT queries. Similar records were not considered in the result in...

3 minutes read.

How to Update Table in SQL?

How to Update Table in SQL Introduction UPDATE query is used to update a record in a table. UPDATE is a DML command, which operates on the data of the table and not...

4 minutes read.