The SQL “LIKE” operator is used in a query to search for a specific pattern in a column’s data. It is often used with the “WHERE” clause in a SELECT statement.
The “LIKE” operator allows you to perform pattern matching using wildcard characters. The two commonly used wildcard characters are:
- “%” (percent sign): Represents any sequence of characters.
- “_” (underscore): Represents any single character.
Here’s an example to illustrate the usage of the “LIKE” operator:
SELECT * FROM employees WHERE last_name LIKE 'Sm%';
In this example, the query selects all rows from the “employees” table where the “last_name” column starts with ‘Sm’. The ‘%’ wildcard represents any sequence of characters that can follow ‘Sm’.
You can also use the ‘%’ wildcard at the beginning or end of the pattern or even both. Here are a few more examples:
SELECT * FROM employees WHERE first_name LIKE '%an%';
This query retrieves all rows from the “employees” table where the “first_name” column contains the substring ‘an’ anywhere in the name.
SELECT * FROM employees WHERE email LIKE '[email protected]';
This query selects all rows from the “employees” table where the “email” column has a single character followed by ‘@gmail.com’. The ‘_’ wildcard represents any single character.
It’s important to note that the “LIKE” operator is case-insensitive by default in most database systems. However, this behavior can vary depending on the database engine or the collation settings.
Additionally, some database systems provide additional wildcard characters and functionality beyond the basic ‘%’ and ‘_’ wildcards.