MySQL NOT LIKE Operator

The MySQL NOT LIKE Operator performs precisely opposite to the Like that we discussed earlier. The Not Like operator returns the records whose values are not matching with a given pattern.

For this NOT LIKE operator and Wildcards demo, we used the below-shown data.

Customer Table rows 1

MySQL NOT LIKE Operator Example

In this example, we are using the percentage wildcard along with the Not Like operator. The following query returns all the Customers whose Occupation doesn’t start with M.

SELECT EmpID, 
       `First Name`,
       `Last Name`,
       Qualification,
       Occupation,
       Income,
       Sales,
       HireDate
FROM customer
WHERE Occupation NOT LIKE 'M%';
Example 2

This Not Like query shows the Employees whose Occupation doesn’t end with l.

SELECT EmpID, 
       `First Name`,
       `Last Name`,
       Qualification,
       Occupation,
       Income,
       Sales,
       HireDate
FROM customer
WHERE Occupation NOT LIKE '%l';
Example 3

NOT LIKE Example 2

In this example, we are using the underscore wildcard to match the single character. The below query returns the employees whose Occupation doesn’t contain the second letter as a.

SELECT EmpID, 
       `First Name`,
       `Last Name`,
       Qualification,
       Occupation,
       Income,
       Sales,
       HireDate
FROM customer
WHERE Occupation NOT LIKE '_a%';
MySQL NOT LIKE Operator 4

The following Not Like query selects the employees Whose Occupation starts with M, and the third letter or character should be n. Please refer Like Operator article.

SELECT EmpID, 
       `First Name`,
       `Last Name`,
       Qualification,
       Occupation,
       Income,
       Sales,
       HireDate
FROM customer
WHERE Occupation NOT LIKE '%M_n%';
NOT LIKE Example 5

MySQL NOT LIKE Operator Command Prompt Example

This example shows you the use of Not Like from the command prompt. You can also use any of the above query examples using this Terminal.

SELECT 'a' NOT LIKE 'a', 'a' NOT LIKE ' a', 'a' NOT LIKE 'a ';

SELECT 'Suresh@' NOT LIKE 'Suresh_', 'Suresh@' NOT LIKE '%h_';

SELECT 'Suresh_Babu' NOT LIKE 'Suresh\_%';
Command Prompt Example 6