SQL Tutorial



LOGICAL OPERATORS IN SQL


Logical Operators in SQL

In SQL, logical operators are used to combine multiple conditions in a query. These operators help refine queries by allowing you to specify more complex criteria.

AND / OR Operators

The AND and OR operators are used to combine multiple conditions in the WHERE clause.

SELECT * FROM Employees WHERE Age > 25 AND Department = 'HR';
      

In this example, the query will return all employees who are older than 25 and work in the 'HR' department. If either condition fails, the record won't be selected.

SELECT * FROM Employees WHERE Department = 'HR' OR Department = 'IT';
      

In this example, the query will return employees working in either the 'HR' or 'IT' department.

NOT Operator

The NOT operator is used to negate a condition. It allows you to select records that do not meet a specific condition.

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

This query will return all employees whose department is not 'HR'.

Tips for Using Logical Operators

  • Use AND to combine conditions that must all be true.
  • Use OR to combine conditions where at least one must be true.
  • Use NOT to negate a condition.
  • Parentheses can help group conditions and change the order of evaluation, making the logic clearer.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review