SQL AND, OR and NOT

In SQL, logical operators such as AND, OR, and NOT can be used in combination with the WHERE clause to create more complex search conditions.

The AND operator is used to combine two or more conditions, and it retrieves rows where both conditions are true. The basic syntax of a SELECT statement using the AND operator is as follows:

refe‮igi:ot r‬ftidea.com
SELECT column1, column2, ..., columnN
FROM table_name
WHERE condition1 AND condition2;

In this syntax, condition1 and condition2 are the expressions that are joined by the AND operator. Only the rows that satisfy both conditions are returned in the result set.

For example, to retrieve all the data from the employees table where the salary column is greater than or equal to 50000 and the department column is 'Sales', you would use the following SELECT statement with the AND operator:

SELECT *
FROM employees
WHERE salary >= 50000 AND department = 'Sales';

This statement retrieves all the rows from the employees table where the value of the salary column is greater than or equal to 50000 and the value of the department column is 'Sales'.

The OR operator is used to combine two or more conditions, and it retrieves rows where at least one of the conditions is true. The basic syntax of a SELECT statement using the OR operator is as follows:

SELECT column1, column2, ..., columnN
FROM table_name
WHERE condition1 OR condition2;

In this syntax, condition1 and condition2 are the expressions that are joined by the OR operator. Rows that satisfy either condition are returned in the result set.

For example, to retrieve all the data from the employees table where the salary column is greater than or equal to 50000 or the department column is 'Sales', you would use the following SELECT statement with the OR operator:

SELECT *
FROM employees
WHERE salary >= 50000 OR department = 'Sales';

This statement retrieves all the rows from the employees table where the value of the salary column is greater than or equal to 50000 or the value of the department column is 'Sales'.

The NOT operator is used to negate a condition, and it retrieves rows where the condition is false. The basic syntax of a SELECT statement using the NOT operator is as follows:

SELECT column1, column2, ..., columnN
FROM table_name
WHERE NOT condition;

In this syntax, condition is the expression that is negated by the NOT operator. Rows that do not satisfy the condition are returned in the result set.

For example, to retrieve all the data from the employees table where the department column is not 'Sales', you would use the following SELECT statement with the NOT operator:

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

This statement retrieves all the rows from the employees table where the value of the department column is not 'Sales'.