SQL Logical Operator
In SQL, Logical Operators are used to combine multiple conditions within the WHERE clause. These operators help retrieve records that meet specific complex criteria, improving data filtering in your queries.
What Are Logical Operators?
Logical operators evaluate conditions and return TRUE, FALSE, or UNKNOWN for each row. They are used to control which rows appear in the final result set based on one or more logical conditions.
Logical AND Operator
Returns rows only when all conditions are satisfied.
Customer Table
Example:
Output:
Logical OR Operator
Returns rows if at least one condition is true.
Customer Table
Example:
Output:
Logical NOT Operator
Returns rows where the condition is NOT true.
Customer Table
Example:
Output:
Logical operators evaluate conditions and return TRUE, FALSE, or UNKNOWN for each row. They are used to control which rows appear in the final result set based on one or more logical conditions.
Types of SQL Logical Operators
Operator | Description |
---|---|
AND | Returns TRUE only if all conditions are true |
OR | Returns TRUE if any condition is true |
NOT | Reverses the result of a condition |
Logical AND Operator
Returns rows only when all conditions are satisfied.
Customer Table
Customer ID | Name | Postal Code | Country |
---|---|---|---|
2 | Shiva | 05021 | America |
4 | Hanuman | 05023 | Mexico |
1 | Dhruva | 05026 | Japan |
3 | Ganesh | 05024 | Mexico |
Example:
SELECT *
FROM Customer
WHERE Country = 'Mexico' AND CustomerID > 2;
Output:
Customer ID | Name | Postal Code | Country |
---|---|---|---|
4 | Hanuman | 05023 | Mexico |
3 | Ganesh | 05024 | Mexico |
Explanation: Shows customers from Mexico whose CustomerID is greater than 2.
Logical OR Operator
Returns rows if at least one condition is true.
Customer Table
Customer ID | Name | City | Country |
---|---|---|---|
1 | James | Paris | Germany |
2 | William | Lyon | Italy |
3 | Daniel | Berlin | France |
4 | Oliver | Marseille | Germany |
Example:
SELECT *
FROM Customer
WHERE Country = 'Germany' OR City = 'Berlin';
Output:
Customer ID | Name | City | Country |
---|---|---|---|
1 | James | Paris | Germany |
3 | Daniel | Berlin | France |
4 | Oliver | Marseille | Germany |
Explanation: Displays records where the country is Germany or the city is Berlin.
Logical NOT Operator
Returns rows where the condition is NOT true.
Customer Table
ID | Name | City | Country |
---|---|---|---|
1 | James | Paris | Germany |
2 | Daniel | Berlin | Italy |
3 | Oliver | Marseille | Germany |
4 | Rose | Belgium | France |
Example:
SELECT *
FROM Customer
WHERE NOT Country = 'Germany';
Output:
ID | Name | City | Country |
---|---|---|---|
2 | Daniel | Berlin | Italy |
4 | Rose | Belgium | France |
Explanation: Displays customers who do not belong to Germany.
Key Point
- Use AND when all conditions must be true.
- Use OR when at least one condition must be true.
- Use NOT to exclude specific records.
- Logical operators are essential for writing complex and precise SQL queries.