You are on page 1of 9

 Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C

MySQL AND, OR and NOT


Operators
❮ Previous Next ❯

The MySQL AND, OR and NOT Operators


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

The AND and OR operators are used to filter records based on more than one condition:

The AND operator displays a record if all the conditions separated by AND are
TRUE.
The OR operator displays a record if any of the conditions separated by OR is
TRUE.

The NOT operator displays a record if the condition(s) is NOT TRUE.

AND Syntax

SELECT column1, column2, ...


FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
OR Syntax
 Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C

SELECT column1, column2, ...


FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

NOT Syntax

SELECT column1, column2, ...


FROM table_name
WHERE NOT condition;

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

CustomerID CustomerName ContactName Address City

1 Alfreds Maria Anders Obere Str. 57 Berlin


Futterkiste

2 Ana Trujillo Ana Trujillo Avda. de la México D.F


Emparedados y Constitución
helados 2222

3 Antonio Moreno Antonio Moreno Mataderos México D.F


Taquería 2312

4 Around the Horn Thomas Hardy 120 Hanover London


Sq.

5 Berglunds Christina Berguvsvägen Luleå


snabbköp Berglund 8
6 Tutorials  Blauer
See
Exercises 
Delikatessen
Hanna Moos
Services 
Forsterstr.
 57 Mannheim
Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C

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

Example Get your own SQL Server

SELECT * FROM Customers


WHERE Country = 'Germany' AND City = 'Berlin';

Try it Yourself »

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

Example
SELECT * FROM Customers
WHERE City = 'Berlin' OR City = 'Stuttgart';

Try it Yourself »

The following SQL statement selects all fields from "Customers" where country is
"Germany" OR "Spain":
 Tutorials 
Example
Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
SELECT * FROM Customers
WHERE Country = 'Germany' OR Country = 'Spain';

Try it Yourself »

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

Example
SELECT * FROM Customers
WHERE NOT Country = 'Germany';

Try it Yourself »

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):

Example
SELECT * FROM Customers
WHERE Country = 'Germany' AND (City = 'Berlin' OR City = 'Stuttgart');
Try it Yourself »
Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
The following SQL statement selects all fields from "Customers" where country is NOT
"Germany" and NOT "USA":

Example

SELECT * FROM Customers


WHERE NOT Country = 'Germany' AND NOT Country = 'USA';

Try it Yourself »

Test Yourself With Exercises

Exercise:
Select all records where the City column has the value 'Berlin' and the PostalCode
column has the value 12209.

* FROM Customers
City = 'Berlin'
= 12209;

Submit Answer »
Start the Exercise
 Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C

❮ Previous Log in to track progress Next ❯

COLOR PICKER


 Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C

 SPACES UPGRADE NEWSLETTER

GET CERTIFIED REPORT ERROR


Top Tutorials Top References
 Tutorials 
HTML Tutorial
Exercises  Services  
HTML Reference
Sign Up Log in

HTML
 CSS CSS Tutorial
JAVASCRIPT SQL PYTHON CSS Reference PHP
JAVA HOW TO W3.CSS C
JavaScript Tutorial JavaScript Reference
How To Tutorial SQL Reference
SQL Tutorial Python Reference
Python Tutorial W3.CSS Reference
W3.CSS Tutorial Bootstrap Reference
Bootstrap Tutorial PHP Reference
PHP Tutorial HTML Colors
Java Tutorial Java Reference
C++ Tutorial Angular Reference
jQuery Tutorial jQuery Reference

Top Examples Get Certified


HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
W3.CSS Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate

    FORUM ABOUT
W3Schools is optimized for learning and training. Examples might be simplified to
improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of
use, cookie and privacy policy.

Copyright 1999-2023 by Refsnes Data. All Rights Reserved. W3Schools is Powered by


W3.CSS.

 Tutorials  Exercises  Services   Sign Up Log in

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C

You might also like