SQL AND, OR, and NOT Operators Explained: Complete Beginner's Guide with Examples


SQL AND, OR, and NOT Operators

When working with SQL, you'll often need to filter data based on one or more conditions. The AND, OR, and NOT operators are logical operators that help you combine or modify conditions in a WHERE clause.

These operators allow you to create more powerful and flexible SQL queries, making it easier to retrieve exactly the records you need.

In this tutorial, you'll learn how the SQL AND, OR, and NOT operators work, when to use each one, and how to combine them effectively with practical examples.

What Are SQL Logical Operators?

Logical operators are keywords used to combine multiple conditions in SQL.

The three most commonly used logical operators are:

OperatorDescription
ANDReturns rows only if all conditions are true.
ORReturns rows if at least one condition is true.
NOTReverses a condition by returning rows where the condition is false.

These operators are most commonly used with the WHERE clause.


Sample Customers Table

The examples in this tutorial use the following table.

CustomerIDFirstNameCountryAgeSalary
1JohnUSA283500
2AliceCanada344800
3DavidKenya304200
4GraceTanzania253100
5JamesUganda406000
6SophiaKenya273900

SQL AND Operator

The AND operator returns rows only when every condition is true.

Syntax

SELECT column_name
FROM table_name
WHERE condition1
AND condition2;

Example 1

Retrieve customers from Kenya who are older than 25.

SELECT *
FROM Customers
WHERE Country = 'Kenya'
AND Age > 25;

Result

FirstNameCountryAge
DavidKenya30
SophiaKenya27

Example 2

Find customers earning more than 4,000 and older than 30.

SELECT *
FROM Customers
WHERE Salary > 4000
AND Age > 30;

Result

FirstNameSalary
Alice4800
James6000

SQL OR Operator

The OR operator returns rows when at least one condition is true.

Syntax

SELECT column_name
FROM table_name
WHERE condition1
OR condition2;

Example 1

Retrieve customers from Kenya or Tanzania.

SELECT *
FROM Customers
WHERE Country = 'Kenya'
OR Country = 'Tanzania';

Result

FirstNameCountry
DavidKenya
GraceTanzania
SophiaKenya

Example 2

Find customers younger than 26 or earning more than 5,000.

SELECT *
FROM Customers
WHERE Age < 26
OR Salary > 5000;

SQL NOT Operator

The NOT operator reverses a condition.

Syntax

SELECT column_name
FROM table_name
WHERE NOT condition;

Example

Retrieve customers who are not from the USA.

SELECT *
FROM Customers
WHERE NOT Country = 'USA';

Combining AND and OR

You can combine multiple logical operators in one query.

Example

Retrieve customers from Kenya who are older than 25, or customers from Uganda.

SELECT *
FROM Customers
WHERE (Country = 'Kenya' AND Age > 25)
OR Country = 'Uganda';

Result

FirstNameCountry
DavidKenya
SophiaKenya
JamesUganda

Using parentheses makes the query easier to read and ensures SQL evaluates the conditions in the intended order.


Using NOT with AND

Retrieve customers who are not from Kenya and are older than 30.

SELECT *
FROM Customers
WHERE NOT Country = 'Kenya'
AND Age > 30;

Using NOT with OR

Retrieve customers who are neither from Canada nor the USA.

SELECT *
FROM Customers
WHERE NOT (Country = 'Canada'
OR Country = 'USA');

Operator Precedence

SQL evaluates logical operators in this order:

  1. NOT

  2. AND

  3. OR

For example:

SELECT *
FROM Customers
WHERE Country = 'Kenya'
OR Country = 'Uganda'
AND Age > 30;

This query evaluates the AND condition before the OR.

To avoid confusion, use parentheses:

SELECT *
FROM Customers
WHERE (Country = 'Kenya'
OR Country = 'Uganda')
AND Age > 30;

Practical Examples

Find customers between the ages of 25 and 35.

SELECT *
FROM Customers
WHERE Age >= 25
AND Age <= 35;

Find customers from Kenya or Tanzania with salaries above 3,500.

SELECT *
FROM Customers
WHERE (Country = 'Kenya'
OR Country = 'Tanzania')
AND Salary > 3500;

Exclude customers younger than 18.

SELECT *
FROM Customers
WHERE NOT Age < 18;

Common Mistakes

Forgetting Parentheses

Incorrect:

SELECT *
FROM Customers
WHERE Country = 'Kenya'
OR Country = 'Uganda'
AND Salary > 4000;

This may return unexpected results because AND is evaluated before OR.

Correct:

SELECT *
FROM Customers
WHERE (Country = 'Kenya'
OR Country = 'Uganda')
AND Salary > 4000;

Using Too Many OR Conditions

Instead of:

SELECT *
FROM Customers
WHERE Country = 'Kenya'
OR Country = 'Uganda'
OR Country = 'Tanzania';

Use:

SELECT *
FROM Customers
WHERE Country IN ('Kenya', 'Uganda', 'Tanzania');

The IN operator is shorter, easier to read, and easier to maintain.


Best Practices

  • Use AND when every condition must be true.

  • Use OR when any condition can be true.

  • Use NOT to exclude matching records.

  • Add parentheses when combining multiple logical operators.

  • Keep conditions simple and readable.

  • Test complex queries with a SELECT statement before using the same conditions in UPDATE or DELETE statements.

Conclusion

The SQL AND, OR, and NOT operators are essential tools for filtering and retrieving data from relational databases. They allow you to combine multiple conditions, exclude specific records, and build flexible queries that return exactly the information you need.

By understanding how these logical operators work—and by using parentheses to control evaluation order—you can write more accurate, readable, and efficient SQL queries. As your SQL skills grow, these operators will become a key part of your everyday database work.


Post a Comment

Previous Post Next Post

Ad 1

Ad 2