SQL AND, OR and NOT

The SQL AND, OR and NOT Operators

The WHERE clause can be combined with AND, OR, and NOT operators.

The AND and OR operators are used to filter records based on more than one condition:
  • The AND operator displays a record if all the conditions separated by AND are TRUE.
  • The OR operator displays a record if any of the conditions separated by OR is TRUE.
The NOT operator displays a record if the condition(s) is NOT TRUE.

AND Syntax

SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

The AND operator in SQL is used to combine two or more conditions and returns records that meet all of the conditions. The syntax for using the AND operator in SQL is as follows:

Read More:- SQL WHERE Clause

In this syntax, column1, column2, ... represent the columns that you want to retrieve from the table_name. The WHERE clause is used to specify the conditions that must be met for a record to be included in the result set. The AND operator is used to combine multiple conditions, and each condition must be enclosed in parentheses.

For example, suppose you have a table called "employees" that contains the following columns: "employee_id", "first_name", "last_name", "job_title", and "salary". To retrieve records of employees who are managers and have a salary greater than $50,000, you can use the following SQL statement:

SELECT employee_id, first_name, last_name, job_title, salary
FROM employees
WHERE job_title = 'Manager' AND salary > 50000;

This statement will retrieve records of employees who have a job title of "Manager" AND a salary greater than $50,000.

It is important to note that when using the AND operator, all conditions must be true for a record to be included in the result set. If any of the conditions are false, the record will not be included in the result set.

Additionally, the AND operator can be used in conjunction with other SQL operators to create more complex conditions. For example, you can use the AND operator with the OR operator to create conditions that return records that meet one condition or another condition, or both. You can also use the NOT operator to negate a condition.

In conclusion, the AND operator in SQL is an essential tool for filtering and retrieving data from relational databases. By combining multiple conditions using the AND operator, you can create more specific conditions that retrieve only the data that meets your needs.

SQL AND, OR and NOT

OR Syntax
The OR operator in SQL is used to combine two or more conditions and returns records that meet at least one of the conditions. The syntax for using the OR operator in SQL is as follows:

SQL AND, OR and NOT

In this syntax, column1, column2, ... represent the columns that you want to retrieve from the table_name. The WHERE clause is used to specify the conditions that must be met for a record to be included in the result set. The OR operator is used to combine multiple conditions, and each condition must be enclosed in parentheses.

For example, suppose you have a table called "employees" that contains the following columns: "employee_id", "first_name", "last_name", "job_title", and "department_id". To retrieve records of employees who are managers or who work in the finance department, you can use the following SQL statement:

SELECT employee_id, first_name, last_name, job_title, department_id
FROM employees
WHERE job_title = 'Manager' OR department_id = 5;

This statement will retrieve records of employees who have a job title of "Manager" OR work in the finance department, which has a department_id of 5.

It is important to note that when using the OR operator, at least one condition must be true for a record to be included in the result set. If all of the conditions are false, the record will not be included in the result set.

Additionally, the OR operator can be used in conjunction with other SQL operators to create more complex conditions. For example, you can use the OR operator with the AND operator to create conditions that return records that meet one condition AND another condition, or either condition. You can also use the NOT operator to negate a condition.

In conclusion, the OR operator in SQL is an essential tool for filtering and retrieving data from relational databases. By combining multiple conditions using the OR operator, you can create more flexible conditions that retrieve the data that meets your needs.

NOT Syntax
The SQL NOT operator is a logical operator used to negate or reverse the meaning of a condition. It can be used in conjunction with other operators such as AND and OR to create complex conditional expressions. The NOT operator is used to return the opposite of the logical expression it is applied to.

The syntax for using the NOT operator in SQL varies depending on the context in which it is used. One common use case is in the WHERE clause of a SELECT statement. In this case, the NOT operator is used to exclude rows that match a certain condition. For example, consider the following query:

SELECT * FROM employees
WHERE NOT department = 'Sales';

This query selects all employees whose department is not Sales. The NOT operator negates the condition 'department = 'Sales'', so only employees whose department is something other than Sales are returned.

The NOT operator can also be used with the EXISTS and IN operators. For example, consider the following query:

SELECT * FROM employees
WHERE NOT EXISTS (
SELECT * FROM orders
WHERE orders.employee_id = employees.id
);

This query selects all employees who do not have any associated orders. The NOT operator negates the EXISTS condition, so only employees who do not have any matching rows in the orders table are returned.

In addition to the basic NOT operator, there are also several other variations of the operator in SQL. For example, the NOT LIKE operator can be used to exclude rows that match a certain pattern. The NOT BETWEEN operator can be used to exclude rows that fall within a certain range of values. The NOT IN operator can be used to exclude rows that match any value in a specified list.

In summary, the SQL NOT operator is a powerful tool for creating complex conditional expressions. By negating the meaning of a condition, the NOT operator allows you to exclude or filter out rows that match certain criteria. With its flexibility and versatility, the NOT operator is an essential tool in any SQL developer's toolbox.

SQL AND, OR and NOT

AND Example
The following SQL statement selects all fields from "Customers" where country is "Germany" AND city is "Berlin":

Example

SQL AND, OR and NOT

OR Example
The following SQL statement selects all fields from "Customers" where city is "Berlin" OR "München":

SQL AND, OR and NOT

The following SQL statement selects all fields from "Customers" where country is "Germany" OR "Spain":

SQL AND, OR and NOT

NOT Example
The following SQL statement selects all fields from "Customers" where country is NOT "Germany":

SQL AND, OR and NOT

Combining AND, OR and NOT
You can also combine the AND, OR and NOT operators.

The following SQL statement selects all fields from "Customers" where country is "Germany" AND city must be "Berlin" OR "München" (use parenthesis to form complex expressions):

SQL AND, OR and NOT

The following SQL statement selects all fields from "Customers" where country is NOT "Germany" and NOT "USA":

SQL AND, OR and NOT
Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.