Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Logical Operators (AND, OR, NOT) in SQL


In SQL, logical operators are used to combine multiple conditions in a WHERE clause to filter records based on specific criteria. These operators allow for more complex queries by enabling you to create multiple conditions that must be met or any one of them that can be true.

1. Overview of Logical Operators

There are three primary logical operators in SQL:

These operators are often used in conjunction with comparison operators like =, !=, >, and others to filter data in more advanced ways.

2. Using AND in SQL

The AND operator is used to combine two or more conditions. All conditions connected with AND must be true for a record to be included in the result set.

Example: Using AND to Filter Data

      -- Find employees who work in the 'HR' department and have a salary greater than 50000
      SELECT FirstName, LastName, Department, Salary
      FROM Employees
      WHERE Department = 'HR' AND Salary > 50000;
    

This query retrieves the first name, last name, department, and salary of employees who work in the 'HR' department and have a salary greater than 50,000. Both conditions (department and salary) must be true for the employee to be included in the result set.

3. Using OR in SQL

The OR operator is used to combine multiple conditions where at least one condition must be true for a record to be included in the result set.

Example: Using OR to Filter Data

      -- Find employees who work in the 'HR' department or have a salary greater than 50000
      SELECT FirstName, LastName, Department, Salary
      FROM Employees
      WHERE Department = 'HR' OR Salary > 50000;
    

This query retrieves the first name, last name, department, and salary of employees who either work in the 'HR' department or have a salary greater than 50,000. If any one of the conditions is true, the employee will be included in the result set.

4. Using NOT in SQL

The NOT operator is used to negate a condition. It returns the opposite of the condition it precedes, so if a condition is true, NOT makes it false, and if the condition is false, NOT makes it true.

Example: Using NOT to Exclude Data

      -- Find employees who do not work in the 'HR' department
      SELECT FirstName, LastName, Department
      FROM Employees
      WHERE NOT Department = 'HR';
    

This query retrieves the first name, last name, and department of employees who do not work in the 'HR' department. The NOT operator reverses the condition, so it excludes all employees who are in the 'HR' department.

5. Combining AND, OR, and NOT in SQL

Logical operators can be combined to create more complex conditions. When combining operators, it is important to use parentheses to group conditions properly and ensure the correct order of evaluation.

Example: Combining AND, OR, and NOT

      -- Find employees who either work in 'HR' and have a salary greater than 50000, 
      -- or work in 'Finance' but do not have a salary greater than 60000
      SELECT FirstName, LastName, Department, Salary
      FROM Employees
      WHERE (Department = 'HR' AND Salary > 50000) 
         OR (Department = 'Finance' AND NOT Salary > 60000);
    

This query retrieves employees based on two sets of conditions:

The parentheses ensure the correct order of evaluation for the conditions.

6. Precedence of Logical Operators

When combining multiple logical operators, SQL evaluates the operators according to a specific precedence order:

This precedence order is important to keep in mind when writing complex queries. Parentheses can be used to override the default precedence if needed.

Example: Logical Operator Precedence

      -- Find employees who work in 'HR' and either have a salary greater than 50000 or have been with the company for more than 5 years
      SELECT FirstName, LastName, Department, Salary, YearsAtCompany
      FROM Employees
      WHERE Department = 'HR' 
        AND (Salary > 50000 OR YearsAtCompany > 5);
    

This query retrieves employees who work in the 'HR' department and either have a salary greater than 50,000 or have been with the company for more than 5 years. The parentheses ensure that the OR condition is evaluated first, followed by the AND condition.

7. Conclusion

Logical operators AND, OR, and NOT are essential tools for building complex queries in SQL. By combining conditions effectively, you can filter data in flexible and powerful ways. Understanding how to use these logical operators will help you write more efficient and precise SQL queries to meet your data retrieval needs.



Advertisement

Advertisement

Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java