There three type of logical operators – AND OR NOT

Let’s consider a table for operators operations of AND operator.

AND operator

The AND operator is used to combine two or more conditions and return true if all the conditions are true.

Syntax –

Syntax –

For example, the following query would return all records where both the gender is ‘M’ AND their age must be greater than 55.

For example, the following query would return all records where both the gender is ‘F’ AND their age must be greater than 55.

Note: you can use multiple AND operators also. How!!! Let’s see below:

Let’s see some another example:

Let’s take another example:

OR operator

The OR operator is used to combine two or more conditions and return true if any of the conditions are true.

Syntax

Let’s consider a table for operators operations of OR operator.

For example, the following query would return all records where loc either ‘kolkata’ or ‘california’

NOT operator

The NOT operator is used to negate a condition and return true if the condition is not true.

Syntax –

Consider this table:

For example, the following query would return name,age where age is not greater than 35.

For example, the following query would return name,loc,age where loc is not ‘kolkata’.

Let’s take another example:

Let’s work with IN operator with NOT operator

The IN operator in MySQL is used to specify multiple values in a WHERE clause. It allows you to specify multiple values in a single statement, making it more concise and easier to read than using multiple OR conditions. For example, you can use the IN operator to find all records where a column matches any of a list of specified values. Here’s an example:

This query will return all records where column_name matches value1, value2, or value3.

Note, we can use ‘NOT IN’ operator in sub-queries. So the syntax:

Let’s consider a student’s table

Let’s implement IN operator into this table:

Let’s implement ‘NOT IN’ operator into this table:

Let’s understand by another example: show all students who not belongs from ‘bakura’