Logical operators in MySQL are used in the WHERE clause to combine multiple conditions and filter rows based on complex logic. These operators include AND, OR, NOT, and others that help in defining relationships between conditions.


List of Logical Operators in MySQL

OperatorDescription
ANDReturns TRUE if all conditions are true
ORReturns TRUE if any condition is true
NOTReturns TRUE if the condition is false
XORReturns TRUE if one (and only one) condition is true
IS TRUEChecks if a condition evaluates to true
IS FALSEChecks if a condition evaluates to false
IS NULLChecks if a column has a NULL value

1. AND Operator

The AND operator combines multiple conditions and returns rows only if all conditions are true.

Syntax

SELECT column1, column2 
FROM table_name
WHERE condition1 AND condition2;

Example

Find employees who are older than 30 and work in the Sales department.

SELECT * FROM Employees WHERE Age > 30 AND Department = 'Sales';

2. OR Operator

The OR operator combines multiple conditions and returns rows if any condition is true.

Syntax

SELECT column1, column2
FROM table_name
WHERE condition1 OR condition2;

Example

Find employees who are older than 40 or earn more than 60,000.

SELECT * FROM Employees WHERE Age > 40 OR Salary > 60000;

3. NOT Operator

The NOT operator negates a condition, returning rows where the condition is false.

Syntax

SELECT column1, column2
FROM table_name
WHERE NOT condition;

Example

Find employees who are not in the IT department.

SELECT * FROM Employees WHERE NOT Department = 'IT';

4. XOR Operator

The XOR operator returns TRUE if one (and only one) condition is true. If both conditions are true or both are false, it returns FALSE.

Syntax

SELECT column1, column2
FROM table_name
WHERE condition1 XOR condition2;

Example

Find employees who are either older than 30 or work in HR, but not both.

SELECT * FROM Employees WHERE Age > 30 XOR Department = 'HR';

5. IS TRUE and IS FALSE

These operators explicitly check whether a condition evaluates to TRUE or FALSE.

Syntax

SELECT column1, column2
FROM table_name
WHERE condition IS TRUE;

Example

Find employees where the salary comparison condition is true.

SELECT * FROM Employees WHERE (Salary > 60000) IS TRUE;

6. Combining Logical Operators

Logical operators can be combined to create more complex conditions. Use parentheses () to group conditions and control precedence.

Example

Find employees who are in the IT or Sales department and earn more than 50,000.

SELECT * FROM Employees 
WHERE (Department = 'IT' OR Department = 'Sales') AND Salary > 50000;

Precedence:

  • NOT has the highest precedence.
  • AND has a higher precedence than OR.

Example Table: Employees

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
1JohnSmith30Sales50000USA
2JaneDoe45HR65000UK
3SarahJohnson28IT55000USA
4MichaelBrown50Sales70000Canada

Examples with Output

  1. AND Example
SELECT * FROM Employees WHERE Age > 30 AND Department = 'Sales';

Output:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
4MichaelBrown50Sales70000Canada
  1. OR Example
SELECT * FROM Employees WHERE Age > 40 OR Salary > 60000;

Output:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
2JaneDoe45HR65000UK
4MichaelBrown50Sales70000Canada
  1. NOT Example
SELECT * FROM Employees WHERE NOT Department = 'IT';

Output:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
1JohnSmith30Sales50000USA
2JaneDoe45HR65000UK
4MichaelBrown50Sales70000Canada
  1. Combined Example
SELECT * FROM Employees 
WHERE (Department = 'IT' OR Department = 'Sales') AND Salary > 50000;

Output:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
3SarahJohnson28IT55000USA
4MichaelBrown50Sales70000Canada

Conclusion

Logical operators allow you to build advanced filtering criteria in MySQL queries. By understanding the precedence of operators and how to combine them effectively, you can perform complex data retrieval operations.

Logical operators in MySQL are used in the WHERE clause to combine multiple conditions and filter rows based on complex logic. These operators include AND, OR, NOT, and others that help in defining relationships between conditions.


List of Logical Operators in MySQL

OperatorDescription
ANDReturns TRUE if all conditions are true
ORReturns TRUE if any condition is true
NOTReturns TRUE if the condition is false
XORReturns TRUE if one (and only one) condition is true
IS TRUEChecks if a condition evaluates to true
IS FALSEChecks if a condition evaluates to false
IS NULLChecks if a column has a NULL value

1. AND Operator

The AND operator combines multiple conditions and returns rows only if all conditions are true.

Syntax

SELECT column1, column2 
FROM table_name
WHERE condition1 AND condition2;

Example

Find employees who are older than 30 and work in the Sales department.

SELECT * FROM Employees WHERE Age > 30 AND Department = 'Sales';

2. OR Operator

The OR operator combines multiple conditions and returns rows if any condition is true.

Syntax

SELECT column1, column2
FROM table_name
WHERE condition1 OR condition2;

Example

Find employees who are older than 40 or earn more than 60,000.

SELECT * FROM Employees WHERE Age > 40 OR Salary > 60000;

3. NOT Operator

The NOT operator negates a condition, returning rows where the condition is false.

Syntax

SELECT column1, column2
FROM table_name
WHERE NOT condition;

Example

Find employees who are not in the IT department.

SELECT * FROM Employees WHERE NOT Department = 'IT';

4. XOR Operator

The XOR operator returns TRUE if one (and only one) condition is true. If both conditions are true or both are false, it returns FALSE.

Syntax

SELECT column1, column2
FROM table_name
WHERE condition1 XOR condition2;

Example

Find employees who are either older than 30 or work in HR, but not both.

SELECT * FROM Employees WHERE Age > 30 XOR Department = 'HR';

5. IS TRUE and IS FALSE

These operators explicitly check whether a condition evaluates to TRUE or FALSE.

Syntax

SELECT column1, column2
FROM table_name
WHERE condition IS TRUE;

Example

Find employees where the salary comparison condition is true.

SELECT * FROM Employees WHERE (Salary > 60000) IS TRUE;

6. Combining Logical Operators

Logical operators can be combined to create more complex conditions. Use parentheses () to group conditions and control precedence.

Example

Find employees who are in the IT or Sales department and earn more than 50,000.

SELECT * FROM Employees 
WHERE (Department = 'IT' OR Department = 'Sales') AND Salary > 50000;

Precedence:

  • NOT has the highest precedence.
  • AND has a higher precedence than OR.

Example Table: Employees

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
1JohnSmith30Sales50000USA
2JaneDoe45HR65000UK
3SarahJohnson28IT55000USA
4MichaelBrown50Sales70000Canada

Examples with Output

  1. AND Example
SELECT * FROM Employees WHERE Age > 30 AND Department = 'Sales';

Output:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
4MichaelBrown50Sales70000Canada
  1. OR Example
SELECT * FROM Employees WHERE Age > 40 OR Salary > 60000;

Output:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
2JaneDoe45HR65000UK
4MichaelBrown50Sales70000Canada
  1. NOT Example
SELECT * FROM Employees WHERE NOT Department = 'IT';

Output:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
1JohnSmith30Sales50000USA
2JaneDoe45HR65000UK
4MichaelBrown50Sales70000Canada
  1. Combined Example
SELECT * FROM Employees 
WHERE (Department = 'IT' OR Department = 'Sales') AND Salary > 50000;

Output:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
3SarahJohnson28IT55000USA
4MichaelBrown50Sales70000Canada

Conclusion

Logical operators allow you to build advanced filtering criteria in MySQL queries. By understanding the precedence of operators and how to combine them effectively, you can perform complex data retrieval operations.