What are SQL Logical Operators?
SQL Logical Operators are used to combine multiple conditions in a query WHERE
or HAVING
clause to refine the data retrieval process. These operators return a Boolean value (TRUE
, FALSE
, or UNKNOWN
) based on the conditions.
Types of Logical Operators in SQL
Operator | Description |
---|---|
AND | Returns TRUE if all conditions are true. |
OR | Returns TRUE if any one condition is true. |
NOT | Negates a condition, returning the opposite Boolean value (TRUE becomes FALSE and vice versa). |
BETWEEN | Checks if a value is within a specified range. |
IN | Checks if a value matches any value in a list. |
EXISTS | Returns TRUE if a subquery returns one or more rows. |
IS NULL | Checks if a value is NULL . |
Using SQL Logical Operators
1. AND
Operator
The AND
operator combines two or more conditions and returns TRUE
only if all conditions are true.
Syntax:
Example: Retrieve employees from the "IT" department with a salary greater than 5000.
2. OR
Operator
The OR
operator combines two or more conditions and returns TRUE
if any one of the conditions is true.
Syntax:
Example: Retrieve employees who are either in the "IT" department or earn more than 7000.
3. NOT
Operator
The NOT
operator reverses the result of a condition. If a condition is TRUE
, NOT
makes it FALSE
and vice versa.
Syntax:
Example: Retrieve employees not in the "Sales" department.
4. Combining AND
, OR
, and NOT
Logical operators can be combined in a query. Use parentheses to ensure the correct order of evaluation.
Example: Retrieve employees who are either in the "IT" department or earn more than 7000 but exclude employees in the "Sales" department.
Logical Operators with Examples
1. Using BETWEEN
The BETWEEN
operator checks if a value lies between two specified values (inclusive).
Example: Retrieve employees earning a salary between 3000 and 7000.
2. Using IN
The IN
operator checks if a value matches any value in a specified list.
Example: Retrieve employees from the "IT", "HR", or "Sales" departments.
3. Using EXISTS
The EXISTS
operator checks if a subquery returns any rows.
Example: Retrieve employees who have placed at least one order.
4. Using IS NULL
The IS NULL
operator checks if a column's value is NULL
.
Example: Retrieve employees without a manager assigned.
Order of Precedence for Logical Operators
When multiple logical operators are used, SQL evaluates them in the following order:
- NOT
- AND
- OR
To override the default precedence, use parentheses.
Example:
Best Practices
- Use Parentheses: Always use parentheses to group conditions and ensure clarity.
- Optimize for Readability: Break long conditions into multiple lines for better readability.
- Avoid Overusing NOT: Replace
NOT
with equivalent positive conditions where possible for better readability.- Instead of
NOT salary > 5000
: Usesalary <= 5000
.
- Instead of
Conclusion
Logical operators in SQL are essential for building complex query conditions. By combining conditions with AND
, OR
, and NOT
, you can filter and retrieve data more effectively. Familiarize yourself with these operators to write powerful SQL queries.