The SQL LIKE condition allows you to use wildcards in the SQL WHERE clause of an SQL statement. This allows you to perform pattern matching. The SQL LIKE condition can be used in any valid SQL statement - SQL SELECT statement, SQL INSERT statement, SQL UPDATE statement, or SQL DELETE statement.
The patterns that you can choose from are:
- % allows you to match any string of any length (including zero length)
- _ allows you to match on a single character
SQL LIKE Condition - Using % wildcard example
Let's explain how the % wildcard works in the SQL LIKE condition. We are going to try to find all of the suppliers whose name begins with 'Hew'.
SELECT * FROM suppliers WHERE supplier_name like 'Hew%';
You can also using the % wildcard multiple times within the same string. For example,
SELECT * FROM suppliers WHERE supplier_name like '%bob%';
In this SQL LIKE condition example, we are looking for all suppliers whose name contains the characters 'bob'.
You could also use the SQL LIKE condition to find suppliers whose name does not start with 'T'.
For example:
SELECT * FROM suppliers WHERE supplier_name not like 'T%';
By placing the not keyword in front of the SQL LIKE condition, you are able to retrieve all suppliers whose name does not start with 'T'.