The document discusses the three logical operators in SQL - AND, OR, and NOT. It defines each operator and provides examples of their usage in SELECT statements. AND returns rows where all conditions are true, OR returns rows where at least one condition is true, and NOT returns rows where the condition is false. Syntax and examples are provided for each operator to illustrate how they function.
2. Objectives
1. To determine what are the Logical Operators in
SQL.
2. To understand the usage of Logical Operators.
3. To understand the syntax of the Logical Operators.
4. To understand the importance of the Logical
Operators.
4. SQLLogical
Operators
There are three Logical Operators namely, AND, OR,
and NOT.
These operators compare two conditions at a time to
determine whether a row can be selected for the
output.
When retrieving data using a SELECT statement, you
can use logical operators in the WHERE clause, which
allows you to combine more than one condition.
5. SQLLogical
Operators
Logical
Operators
Description
OR
For the row to be selected at least one of the
conditions must be true.
AND
For a row to be selected all the specified
conditions must be true.
NOT
For a row to be selected the specified condition
must be false.
8. DemoDatabase
Below is a selection from the "Customers" table in the
Northwind sample database:
CustomerID CustomerNa
me
ContactNam
e
Address City PostalCode Country
1 Alfreds
Futterkiste
Maria
Anders
Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo
Emparedado
s y helados
Ana Trujillo Avda. de la
Constitución
2222
México D.F. 05021 Mexico
3 Antonio
Moreno
Taquería
Antonio
Moreno
Mataderos
2312
México D.F. 05023 Mexico
4 Around the
Horn
Thomas
Hardy
120 Hanover
Sq.
London WA1 1DP UK
5 Berglunds
snabbköp
Christina
Berglund
Berguvsväge
n 8
Luleå S-958 22 Sweden
9. "OR"Logical
Operator
Example
The following SQL statement selects all fields from
"Customers" where city is "Berlin" OR "München":
SELECT * FROM Customers
WHERE City='Berlin' OR City='München';
10. "OR"Operator
Table
The following table describes how logical "OR" operator
selects a row.
Column1
Satisfied?
Column2
Satisfied?
Row Selected
YES YES YES
YES NO YES
NO YES YES
NO NO NO
13. DemoDatabase
Below is a selection from the "Customers" table in the
Northwind sample database:
CustomerID CustomerNa
me
ContactNam
e
Address City PostalCode Country
1 Alfreds
Futterkiste
Maria
Anders
Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo
Emparedado
s y helados
Ana Trujillo Avda. de la
Constitución
2222
México D.F. 05021 Mexico
3 Antonio
Moreno
Taquería
Antonio
Moreno
Mataderos
2312
México D.F. 05023 Mexico
4 Around the
Horn
Thomas
Hardy
120 Hanover
Sq.
London WA1 1DP UK
5 Berglunds
snabbköp
Christina
Berglund
Berguvsväge
n 8
Luleå S-958 22 Sweden
14. "AND"Logical
Operator
Example
The following SQL statement selects all fields from
"Customers" where city is "Berlin" OR "München":
SELECT * FROM Customers
WHERE Country='Germany' AND City='Berlin';
15. “AND"Operator
Table
The following table describes how logical "OR" operator
selects a row.
Column1
Satisfied?
Column2
Satisfied?
Row Selected
YES YES YES
YES NO NO
NO YES NO
NO NO NO
17. Whentouse
“NOT"Logical
Operator?
If you want to find rows that do not satisfy a condition,
you can use the logical operator, NOT. NOT results in
the reverse of a condition. That is, if a condition is
satisfied, then the row is not returned.
18. DemoDatabase
Below is a selection from the "Customers" table in the
Northwind sample database:
CustomerID CustomerNa
me
ContactNam
e
Address City PostalCode Country
1 Alfreds
Futterkiste
Maria
Anders
Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo
Emparedado
s y helados
Ana Trujillo Avda. de la
Constitución
2222
México D.F. 05021 Mexico
3 Antonio
Moreno
Taquería
Antonio
Moreno
Mataderos
2312
México D.F. 05023 Mexico
4 Around the
Horn
Thomas
Hardy
120 Hanover
Sq.
London WA1 1DP UK
5 Berglunds
snabbköp
Christina
Berglund
Berguvsväge
n 8
Luleå S-958 22 Sweden
19. “NOT"Logical
Operator
Example
The following SQL statement selects all fields from
"Customers" where country is NOT "Germany" and
NOT "USA":
SELECT * FROM Customers
WHERE NOT Country='Germany' AND NOT Country='USA';
20. “NOT"Operator
Table
The following table describes how logical "OR" operator
selects a row.
Column1
Satisfied?
NOT
Column1
Satisfied?
Row Selected
YES NO NO
NO YES YES