SQL LIKE and NOT LIKE Operators

The LIKE and NOT LIKE operators in SQL are used to search for specific patterns in a column's data. These operators are often used to search for strings that match a certain pattern, such as a specific word or phrase, or to exclude rows that contain a certain pattern.

LIKE operator syntax

1SELECT column_name 2FROM table_name 3WHERE column_name LIKE pattern;

The pattern in the above syntax can contain wildcard characters, such as % or _, to match zero or more characters, or a single character, respectively.

For example, consider a table named customers with the following columns: id, first_name, last_name, email. If you want to find all customers whose first name starts with the letter "J", you could use the following query:

1SELECT first_name 2FROM customers 3WHERE first_name LIKE 'A%';

This query would return all first names that start with the letter "A".

NOT LIKE operator

The NOT LIKE operator works similarly to the LIKE operator, but it returns rows that do not match the specified pattern. The syntax for using the NOT LIKE operator is as follows:

1SELECT column_name 2FROM table_name 3WHERE column_name NOT LIKE pattern;

For example, if you wanted to find all customers whose first name does not start with the letter "A", you could use the following query:

1SELECT first_name 2FROM customers 3WHERE first_name NOT LIKE 'A%';