SQL Like

HTML
CSS
C#
SQL

SQL Like

The LIKE is a logical operator in the Structured Query Language. This SQL operator is used in the WHERE clause with the following three statements:

  1. SELECT Statement
  2. UPDATE Statement
  3. DELETE Statement

– It filters the records from the columns based on the pattern specified in the SQL query.
– Following are two wildcard characters that are used either in conjunction or independently with the SQL LIKE operator:

  1. % (percent sign): This wildcard character matches zero, one, or more than one character.
  2. _ (underscore sign): This wildcard character matches only one or a single character.

Example:

SELECT column_Name1, column_Name2 …., column_NameN FROM table_Name WHERE column_name LIKE pattern;

 

In this syntax, the pattern is a sequence of characters that have to be searched in that column which is just specified after the WHERE clause.

Let’s take the following Employee table which helps you to analyze the LIKE operator with % sign:

Suppose, you want to filter the records of those employees whose names start with “S”. For this operation, you have to type the following query:

SELECT * FROM Employee WHERE Name LIKE ‘S%’; 

 This query shows the following table:

Suppose, you want to show the name and salary of those employees whose department name starts with “C” and ends with “g”. For this operation, you have to type the following query:

SELECT Name, Emp_Salary FROM Employee WHERE Emp_Dept LIKE ‘C%g’; 

This query shows the following table in the output:

Suppose, you want to show all records of those employees from the above employee table whose Name contains the letter “a” in any position. For this operation, you have to type the following query:

SELECT * FROM Employee WHERE Emp_Dept LIKE ‘%a%’; 

This query shows the following table in the output:

Let’s take the following Student table which helps you to analyze the LIKE operator with _ (underscore) sign:

Suppose, you want to show all records of those students whose Name contains “a” at the second position. For this operation, you have to type the following query with an underscore sign:

SELECT * FROM Student WHERE Name LIKE ‘_a%’; 

This query shows the following table in the output:

Suppose, you want to access records of those students whose names contain at least 3 characters and start with the letter “S”. For this operation, you have to type the following query:

SELECT * FROM Student WHERE Name LIKE ‘S___%’; 

In this query, you have to use the underscore sign three times after the S character. The above query shows the following table in the output:

Suppose, you want to access Roll_No, Name, and Marks of those students whose Marks are 2 digits long and end with ‘5’:

SELECT * FROM Student WHERE Name LIKE ‘_5’; 

The above query shows the following table in the output:

Course Video

NOTE: Practice below questions on site editor.

1.Make a SQL Query to find name like ‘en’ from HumenResources_Department table

Table: (HumanResources_Department)(1 row count)        

Output:

2. Write a SQL Query to find name From Person_ContactType table where name like ‘a___i’.

Table: (Person_ContactType)-(2 row count)

Output:

3. Write SQL Query to get productID, name, color, listprice, from Production_Product table where Name start with ‘B’ and end with ‘e’

Table: (Production_product) (1 row count)

Output:

4. Write SQL Query to get All the columns from Production_Product table where productNumber like

’CA-7457’ 

Table: (Production_Product) (row count 1)

Output:

5. Make a SQL Query to get all the columns from Sales_SalesReason table where name start with ‘R’ and end with ‘w’   
         

Table: (Sales_SalesReason) ( row count 1)      
         

Output: