In SQL, 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 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 "LIKE" Operator returns true Otherwise it returns false.
Here's how we can use the LIKE operator:
SELECT columns FROM tableName WHERE columeName LIKE pattern;
columns: The columns you want to retrieve from the table.
tableName: The name of the table from which you want to retrieve data.
columnName: The column you want to search.
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.
This query retrieves all columns from the products table where the product_name starts with "Lap".
SELECT * FROM products WHERE product_name LIKE 'Lap%';
This query retrieves all columns from the "products" table where the "product_name" ends with "Top".
SELECT * FROM products WHERE product_name LIKE '% Top';
In The below query to retrieve all columns from the `products` table where the "product_name" does not contain "Table" string.
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_';