Reprence Html CSS javascript PHP MySQL Bootsrap

The MySQL AND, OR and NOT Operators

The WHERE clause can be combined with AND, OR, and NOT operators.

13

AND Syntax

14

OR Syntax

15

NOT Syntax

16

Demo Database

The table below shows the complete "Customers" table from the Northwind sample database:

17 18 19

AND Example

The following SQL statement selects all fields from "Customers" where country is "Germany" AND city is "Berlin":



OR Example

The following SQL statement selects all fields from "Customers" where city is "Berlin" OR "Stuttgart":


The following SQL statement selects all fields from "Customers" where country is "Germany" OR "Spain":


NOT Example

The following SQL statement selects all fields from "Customers" where country is NOT "Germany":


Combining AND, OR and NOT

You can also combine the AND, OR and NOT operators.

The following SQL statement selects all fields from "Customers" where country is "Germany" AND city must be "Berlin" OR "Stuttgart" (use parenthesis to form complex expressions):



The following SQL statement selects all fields from "Customers" where country is NOT "Germany" and NOT "USA":