WHERE, HAVING, and JOIN clauses. They allow you to create complex conditions by combining multiple expressions. The most commonly used logical operators are AND, OR, and NOT.
Key Points About Logical Operators
- Combining Conditions: Logical operators are used to combine multiple conditions in a query.
- Common Operators: The most commonly used logical operators are
AND,OR, andNOT. - Order of Evaluation: Conditions are evaluated in the following order:
NOT,AND,OR. Use parentheses to change the order of evaluation. - AND: Returns
TRUEif all conditions are true. - OR: Returns
TRUEif at least one condition is true. - NOT: Negates a condition, returning
TRUEif the condition is false. - EXISTS: Used to test for the existence of rows in a subquery.
- IN: Used to compare a value against a list or subquery.
- LIKE: Used for pattern matching with wildcards (
%and_). - BETWEEN: Used to filter values within a range (inclusive).
- Use Cases: Logical operators are used in
WHERE,HAVING, andJOINclauses to filter and combine data.
Common Logical Operators
AND Operator
TheAND operator returns TRUE if all the conditions separated by AND are true.
OR Operator
TheOR operator returns TRUE if at least one of the conditions separated by OR is true.
NOT Operator
TheNOT operator negates a condition, returning TRUE if the condition is false.
EXISTS Operator
TheEXISTS operator checks if a subquery returns any rows. It returns TRUE if the subquery returns at least one row, otherwise FALSE.
IN Operator
TheIN operator checks if a value matches any value in a list or subquery.
LIKE Operator
TheLIKE operator is used for pattern matching with wildcards:
%: Matches zero or more characters._: Matches exactly one character.
BETWEEN Operator
TheBETWEEN operator checks if a value lies within a specified range (inclusive).
Combining Logical Operators
You can combine multiple logical operators to create complex conditions. Use parentheses to control the order of evaluation. Retrieve employees from Tamil Nadu with a salary greater than ₹50,000 or employees from Puducherry.
Suppose you have a table named
Students that stores student details.
Table: Students
Retrieve students from Tamil Nadu with a percentage greater than 90 or students from Karnataka.
Check if there are any orders placed by customers from Tamil Nadu.
Key Takeaways
- Logical operators (
AND,OR,NOT) are used to combine or negate conditions in SQL queries. - Use parentheses to control the order of evaluation when combining multiple logical operators.
ANDreturnsTRUEif all conditions are true,ORreturnsTRUEif at least one condition is true, andNOTnegates a condition.- EXISTS: Checks if a subquery returns any rows.
- IN: Compares a value against a list or subquery.
- LIKE: Performs pattern matching using wildcards (
%and_). - BETWEEN: Filters values within a specified range (inclusive).
- These operators are essential for writing flexible and efficient queries in SQL.
- Logical operators are essential for creating complex conditions in
WHERE,HAVING, andJOINclauses.