Ad Code

SQL AND, OR and NOT

The SQL AND, OR and NOT Operators




The SQL AND, OR and NOT Operators

In SQL, the AND, OR, and NOT operators are used to combine conditions in the WHERE clause of a SELECT statement. The logical operators AND, OR, and NOT are used to construct complex search conditions.

AND operator: The AND operator is used to retrieve records that meet multiple conditions. It returns true only when both conditions are true. For example, the following query retrieves the names and departments of employees whose salary is greater than 50000 and belong to the 'Sales' department. 

SELECT name, department

FROM employees

WHERE salary > 50000 AND department = 'Sales';

For example, the following SQL statement will return all employees whose age is greater than 30 and whose salary is greater than $50,000: 

SELECT * 

FROM employees

WHERE age > 30 AND salary > 50000;

 OR operator: The OR operator is used to retrieve records that meet at least one of the specified conditions. It returns true when one or both of the conditions are true. For example, the following query retrieves the names and departments of employees whose salary is greater than 50000 or belong to the 'Sales' department.

 

SELECT name, department

FROM employees

WHERE salary > 50000 OR department = 'Sales';

 For example, the following SQL statement will return all employees whose age is greater than 30 or whose salary is greater than $50,000: 

SELECT * FROM employees

WHERE age > 30 OR salary > 50000;

 NOT operator: The NOT operator is used to retrieve records that do not meet a specified condition. It returns true when the specified condition is not true. For example, the following query retrieves the names and departments of employees whose salary is not greater than 50000. 

SELECT name, department

FROM employees

WHERE NOT salary > 50000;

 For example, the following SQL statement will return all employees whose age is not greater than 30: 

SELECT * 

FROM employees

WHERE NOT age > 30;

 These operators can also be combined to create more complex conditions. For example, the following query retrieves the names and departments of employees whose salary is greater than 50000 and do not belong to the 'Sales' department. 

SELECT name, department

FROM employees

WHERE salary > 50000 AND NOT department = 'Sales';

 For example, the following SQL statement will return all employees whose age is greater than 30 and whose salary is greater than $50,000, or whose job title is "Manager": 

SELECT * FROM employees

WHERE (age > 30 AND salary > 50000) 

OR job_title = 'Manager';

 








Post a Comment

0 Comments

Close Menu