SQL LIKE

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

There are two wildcards used in conjunction with the LIKE operator:

  • % – The percent sign represents zero, one, or multiple characters
  • _ – The underscore represents a single character

Note: MS Access uses a question mark (?) instead of the underscore (_).

The percent sign and the underscore can also be used in combinations!

 

LIKE Syntax

SELECT column1, column2, …
FROM table_name
WHERE columnN LIKE pattern;

The basic syntax of % and _ is as follows −

SELECT FROM table_name
WHERE column LIKE \'XXXX%\'

or 

SELECT FROM table_name
WHERE column LIKE \'%XXXX%\'

or

SELECT FROM table_name
WHERE column LIKE \'XXXX_\'

or

SELECT FROM table_name
WHERE column LIKE \'_XXXX\'

or

SELECT FROM table_name
WHERE column LIKE \'_XXXX_\'

Example

The following table has a few examples showing the WHERE part having different LIKE clause with \’%\’ and \’_\’ operators −

Sr.No. Statement & Description
1 WHERE SALARY LIKE \’60%\’

Finds any values that start with 60.

2 WHERE SALARY LIKE \’%60%\’

Finds any values that have 60 in any position.

3 WHERE SALARY LIKE \’_100%\’

Finds any values that have 100 in the second and third positions.

4 WHERE SALARY LIKE \’6_%_%\’

Finds any values that start with 6 and are at least 3 characters in length.

5 WHERE SALARY LIKE \’%9\’

Finds any values that end with 9.

6 WHERE SALARY LIKE \’_8%3\’

Finds any values that have a 8 in the second position and end with a 3.

7 WHERE SALARY LIKE \’7_ _ _9\’

Finds any values in a five-digit number that start with 7 and end with 9.

Scroll to Top