In MySQL, The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. It is particularly useful when you want to perform pattern matching with wildcard characters.
The LIKE operator in SQL is case-sensitive, we can use functions like "LOWER()" or "UPPER()" to perform case-insensitive searches.
Avoid overusing the wildcards "%" Symbol at the beginning of the pattern as it can lead to slow performance due to full table scans.
If the value in the column matches the pattern specified with wildcard characters then the "LIKE" Operator returns true Otherwise it returns false.
Here's how we can use the LIKE operator In MySQL:
SELECT columns FROM tableName WHERE columnName LIKE pattern;
columns: The columns we want to retrieve from the table.
tableName: The name of the table from which we want to retrieve data.
columnName: The column you want to apply search on text.
pattern: The pattern to search for, which can include wildcard characters.
Wildcard characters "% "and "_" are used to perform flexible searching of a substring from start or end.
%: Represents zero or more characters.
_: Represents a single character.
The below query retrieves all columns from the `products` table where the `product_name` starts with "Lap".
SELECT * FROM products WHERE product_name LIKE 'Lap%';
The below query retrieves all columns from the "products" table where the "product_name" ends with "Top".
SELECT * FROM products WHERE product_name LIKE '% Top';
The below query retrieves all columns from the "products" table where the "product_name" does not contain "Table".
SELECT * FROM products WHERE NOT product_name LIKE '%Table%';
In the below query to retrieve all columns from the `products` table whose "product_price" has '99' followed by any single character.
SELECT * FROM products WHERE product_price LIKE '99_';