In MySQL, wildcards are special characters used in conjunction with the LIKE operator in a WHERE clause to perform pattern matching in string comparisons.
The Wildcards clause searches for a single character or any sequence of characters from a specific column from the SQL Table.
Here are the commonly used wildcards:
Represents zero or more characters.
SELECT * FROM products WHERE product_name LIKE 'Tab%';
The above query retrieves all columns from the `products` table where the `product_name` starts with the 'Tab' string.
Search substring from the sequence of string characters from a specific column in Table.
SELECT * FROM products WHERE product_name LIKE '%cloth%';
Using the above query we can retrieve products whose product_name includes 'cloth' anywhere in their product_name.
Represents a single character in the string.
SELECT * FROM products WHERE product_name LIKE 'BAL_';
The above query retrieves all columns from the products table where the product_name starts with 'BAL' followed by any single character.
The below query will find all products whose category name contains 7 characters long and start with 'CLOTH' and end with 'NG'.
SELECT * FROM products WHERE category LIKE 'CLOTH_NG';
[]: Represents any single character within the specified range.
[^]: Represents any single character not in the specified set.
SELECT * FROM products WHERE product_name LIKE 'Desk[1-3]%'; SELECT * FROM products WHERE product_name NOT LIKE '[AE]%';
[]: This query retrieves all columns from the "products" table where the "product_name" starts with 'Desk' followed by a digit between 1 and 3.
[^]: This query retrieves all columns from the "products" table where the "product_name" does not start with 'A' or 'E'.
Hyphen symbol are used inside square brackets to specify a range of characters.
SELECT * FROM products WHERE product_name LIKE '[a-z]%';
The above query finds all products whose `product_name` starts with any lowercase letter.