LIKE Operator in SQL
The LIKE operator in SQL is used to search for a specified pattern in a column. It is particularly useful for string matching. The LIKE operator is often used with wildcard characters:
- % (percent sign): Represents zero, one, or multiple characters.
- _ (underscore): Represents a single character.
Simple Examples Using LIKEExample 1: Using % Wildcard.
SQL Syntax
SELECT first_name, last_name FROM employees WHERE first_name LIKE 'J%';
In this example:
- The query retrieves all employees whose first names start with 'J'.
- The % wildcard matches any sequence of characters following 'J'.
Example 2: Using _ Wildcard.
SQL Syntax
SELECT first_name, last_name FROM employees WHERE first_name LIKE '_a%';
In this example:
- The query retrieves all employees whose first names have 'a' as the second character.
- The _ wildcard matches exactly one character before 'a'.
The LIKE operator with the pattern '___' (three underscores) is used to match any string that is exactly three characters long. The underscore _ wildcard matches exactly one character. So, the query will return all employees whose first names are exactly three characters long.