MySQL AND, OR, IN, NOT IN

0

- Advertisement -

AND, OR, In, NOT In these all are called logical operators:

AND(&&) :

The AND operator allows creating an SQL statement based on two or more conditions being met. It can be used in any valid SQL statement such as select, insert, update, or delete.

The AND operator requires that each condition must be met for the record to be included in the result set.

The MySQL engine will process all rows in a table and display the result only when all of the conditions specified using the AND operator are satisfied.

Syntax :

EX : MySQL Table tblemployees

idEmp_NameEmp_CodeEmp_EmailEmp_JoiningDate
1Anuj Kumar10806121phpgurukulofficial@gmail.com2019-01-01
2Jonh Doe10899781jonhdie@abc.com
2019-01-01
3Sanjeev Kumar1908763sanjv @test.com
2018-06-11

Both queries will produce same output.

Output:

idEmp_NameEmp_CodeEmp_EmailEmp_JoiningDate
1Anuj Kumar10806121phpgurukulofficial@gmail.com2019-01-01

OR (||) :

The OR condition allows creating an SQL statement where records are returned when any one of the conditions are met. It can be used in any valid SQL statement such as select , insert, update, or delete.

The OR condition requires that any of the conditions must be met for the record to be included in the result set.

Syntax:

Ex:

Output:

idEmp_NameEmp_CodeEmp_EmailEmp_JoiningDate
1Anuj Kumar10806121phpgurukulofficial@gmail.com2019-01-01
2Jonh Doe10899781jonhdie@abc.com
2019-01-01

Combining the AND and OR Operator:

The AND and OR conditions can be combined in a single SQL statement. It can be used in any valid SQL statement such as select, insert, update, or delete.

When combining these conditions, it is important to use brackets so that the database knows what order to evaluate each condition.

The MYSQL engine will process all rows in a table and display the result only when all of the conditions specified using the AND operator are satisfied and when any of the conditions specified using the OR operator are satisfied.

Syntax:

Ex:

Output:

idEmp_NameEmp_CodeEmp_EmailEmp_JoiningDate
1Anuj Kumar10806121phpgurukulofficial@gmail.com2019-01-01
2Jonh Doe10899781jonhdie@abc.com
2019-01-01

IN :

 IN() function finds a match in the given arguments.

Syntax:

Ex:

Output:

idEmp_NameEmp_CodeEmp_EmailEmp_JoiningDate
1Anuj Kumar10806121phpgurukulofficial@gmail.com2019-01-01
2Jonh Doe10899781jonhdie@abc.com
2019-01-01

NOT IN:

The  WHERE clause when used together with the NOT IN keyword  DOES NOT affect the rows whose values match the list of values provided in the NOT IN keyword.

Syntax:

Ex :

Output:

idEmp_NameEmp_CodeEmp_EmailEmp_JoiningDate
3Sanjeev Kumar1908763sanjv @test.com
2018-06-11

- Advertisement -

Leave A Reply

Your email address will not be published.