SQL Keyword - LIKE

LIKE Keyword

The LIKE keyword is used in the where clause to match the specified pattern in the column.

There are two wildcard characters that are used with a like clause.

  1. % - Represent Zero, One Or More Characters.
  2. _ - Represent only one character.

Syntax

SELECT * FROM <TABLE_NAME>
WHERE <COLUMN_NAME> LIKE <'SPECIFIED_PATTERN>';

Example 1: Using % Wildcard

SELECT * FROM Employee WHERE Emp_Name LIKE 'A%';

It returns all records whose emp names start with 'A' or 'a'.

SELECT * FROM Employee WHERE Emp_Name LIKE '%A';

It returns all records whose emp names end with 'A' or 'a'.

SELECT * FROM Employee WHERE Emp_Name LIKE 'M%A';

It returns only records whose emp names start with 'M' or 'm' and end with 'A' or 'a'.

Example 2: Using _ Wildcard

SELECT * FROM Employee WHERE Emp_Name LIKE '_ISHA'

It returns only records whose emp name starts with any character and immediately after ends with 'ISHA', for example, MISHA, NISHA, etc.

SELECT * FROM Employee WHERE Emp_Name LIKE 'MILA_'

It returns only records whose emp name starts with 'MILA' and immediately after ending with any character, for example, MILAN, MILAP, etc.

Example 3: Using % and _ Wildcard

SELECT * FROM Employee WHERE Emp_Name LIKE '%S_'

It returns only records whose emp name starts with any character or any number of characters, but whose second last character is a compulsory 'S' and ends with any character.

Example 4: No any wildcard

SELECT * FROM Employee WHERE Emp_Name LIKE 'UDAY'

It returns only records whose emp name should be 'UDAY'.

Summary

The LIKE keyword is used in the where clause to match the specified pattern in the column.