LIKE operator allows you to perform pattern matching in your column in a database table. LIKE is often used with SELECT statement in WHERE clause. MySQL provides you two wildcard characters for use with LIKE, the percentage % and underscore _.
- Percentage (%) wildcard allows you to match any string of zero or more characters
- Underscore (_) allows you to match any string character.
Let’s see with couples of examples which use SQL Like with different wildcard characters.
If we want to retrieve the various ‘Engineer’ roles/titles available in a company, we can use LIKE operator with % wildcard for the term ‘Engineer’ as follows.
SELECT DISTINCT title FROM titles WHERE title LIKE '%Engineer%';
MySQL scans the whole table to find all titles which has the string ‘Engineer’ in them and returns the result.
We can also negate the result by using the NOT operator on LIKE as follows.
SELECT DISTINCT title FROM titles WHERE title NOT LIKE '%Engineer%';