0% found this document useful (0 votes)
75 views

SQL and or Not

swl and or not
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

SQL and or Not

swl and or not
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

w3schools.

com

  HTML CSS MORE  

SQL AND, OR and NOT Operators


❮ Previous Next ❯

The SQL 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

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
Below is a selection from the "Customers" table in the Northwind sample database:

CustomerID CustomerName ContactName Address City PostalCode Country

1 Alfreds Maria Anders Obere Str. 57 Berlin 12209 Germany


Futterkiste

2 Ana Trujillo Ana Trujillo Avda. de la México 05021 Mexico


Emparedados y Constitución D.F.
helados 2222

3 Antonio Moreno Antonio Mataderos México 05023 Mexico


Taquería Moreno 2312 D.F.

4 Around the Horn Thomas 120 Hanover London WA1 1DP UK


Hardy Sq.

5 Berglunds Christina Berguvsvägen Luleå S-958 22 Sweden


snabbköp Berglund 8

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

Example

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 "München":

Example

SELECT * FROM Customers


WHERE City='Berlin' OR City='München';

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 "München" (use parenthesis to form complex expressions):

Example

SELECT * FROM Customers


WHERE Country='Germany' AND (City='Berlin' OR City='München');

Try it Yourself »

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

❮ Previous Next ❯

COLOR PICKER

Exercises

HTML
CSS

JavaScript

SQL

PHP
Python

Bootstrap

jQuery
SHARE

  

CERTIFICATES
HTML
CSS
JavaScript
PHP
jQuery
Bootstrap
XML

Read More »

REPORT ERROR
PRINT PAGE
FORUM
ABOUT

Top 10 Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
SQL Tutorial
PHP Tutorial
jQuery Tutorial
Python Tutorial

Top 10 References
HTML Reference
CSS Reference
JavaScript Reference
W3.CSS Reference
Bootstrap Reference
SQL Reference
PHP Reference
HTML Colors
jQuery Reference
Python Reference

Top 10 Examples
HTML Examples
CSS Examples
JavaScript Examples
How To Examples
W3.CSS Examples
Bootstrap Examples
PHP Examples
jQuery Examples
Angular Examples
XML Examples

Web Certificates
HTML Certificate
CSS Certificate
JavaScript Certificate
jQuery Certificate
PHP Certificate
Bootstrap Certificate
XML Certificate

W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content.
While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 1999-2019 by
Refsnes Data. All Rights Reserved.
Powered by W3.CSS.

You might also like