Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

sql
mysql

What is NOT LIKE operator in SQL?

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

The NOT LIKE operator in SQL is used on a column which is of type varchar. Usually, it is used with % which is used to represent any string value, including the null character \0.

The string we pass on to this operator is not case-sensitive.

svg viewer
Using '%'

This operator can be used for several purposes:

1. Checking a substring

We can use this operator to extract those rows which do NOT have a particular substring. As an example, suppose that we have a Student table as follows:

ID FirstName
1 Alex
2 Bran
3 Chad

The following query returns the rows where FirstName do not contain the letter B:

SELECT * FROM Student
WHERE FirstName NOT LIKE '%B%'

2. Checking the start or end of a string

Another common use of this operator is to exclude those rows in which a string starts, or ends, with a particular string.

The following query returns the rows in which FirstName do not start with A:

SELECT * FROM Student
WHERE FirstName NOT LIKE 'A%'

Similarly, the query below will return the rows in which FirstName do not end with d:

SELECT * FROM Student
WHERE FirstName NOT LIKE '%d'

RELATED TAGS

sql
mysql

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring