Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. SQL Dictionary
  3. AND / OR / NOT

AND / OR / NOT

Since: SQL-92(1992)

Logical operators used to combine multiple conditions for filtering. It is important to use parentheses to clarify the order of evaluation.

Syntax

AND: Retrieves rows that satisfy both conditions.

SELECT column FROM table WHERE condition1 AND condition2;

OR: Retrieves rows that satisfy at least one condition.

SELECT column FROM table WHERE condition1 OR condition2;

NOT: Negates a condition.

SELECT column FROM table WHERE NOT condition;

Use parentheses to explicitly set the order of evaluation.

SELECT column FROM table WHERE condition1 AND (condition2 OR condition3);

Operators

OperatorDescription
ANDReturns true only when both left and right conditions are true.
ORReturns true when either the left or right condition is true.
NOTReverses the truth value of the following condition.
( )Evaluates the conditions inside the parentheses first. Since AND takes precedence over OR, parentheses are used to evaluate in the intended order.

Sample Code

The following 'employees' table is used in the examples below.

employees id name department salary 1 Okabe Rintaro Future Gadget Lab 320000 2 Makise Kurisu Future Gadget Lab 350000 3 Shiina Mayuri Future Gadget Lab 260000 4 Hashida Itaru Future Gadget Lab 280000 4 rows in set

Retrieves employees in the 'Future Gadget Lab' with a salary of 300,000 or more.

sample_and_or_not.sql
SELECT name, department, salary
FROM employees
WHERE department = 'Future Gadget Lab' AND salary >= 300000;
+---------------+-------------------+--------+
| name          | department        | salary |
+---------------+-------------------+--------+
| Okabe Rintaro | Future Gadget Lab | 320000 |
| Makise Kurisu | Future Gadget Lab | 350000 |
+---------------+-------------------+--------+
2 rows in set

Retrieves employees with a salary of 260,000 or 350,000.

sample_and_or_not.sql
SELECT name, salary
FROM employees
WHERE salary = 260000 OR salary = 350000;
+---------------+--------+
| name          | salary |
+---------------+--------+
| Makise Kurisu | 350000 |
| Shiina Mayuri | 260000 |
+---------------+--------+
2 rows in set

Retrieves employees whose salary is not less than 300,000 (i.e., 300,000 or more).

sample_and_or_not.sql
SELECT name, salary
FROM employees
WHERE NOT salary < 300000;
+---------------+--------+
| name          | salary |
+---------------+--------+
| Okabe Rintaro | 320000 |
| Makise Kurisu | 350000 |
+---------------+--------+
2 rows in set

Use parentheses to control evaluation order. Retrieves employees with a salary of 350,000 or more, OR employees in the 'Future Gadget Lab' with a salary of 300,000 or more.

sample_and_or_not.sql
SELECT name, department, salary
FROM employees
WHERE salary >= 350000 OR (department = 'Future Gadget Lab' AND salary >= 300000);
+---------------+-------------------+--------+
| name          | department        | salary |
+---------------+-------------------+--------+
| Okabe Rintaro | Future Gadget Lab | 320000 |
| Makise Kurisu | Future Gadget Lab | 350000 |
+---------------+-------------------+--------+
2 rows in set

Database-Specific Notes

The syntax and operator precedence of AND, OR, and NOT are consistent across all major databases.

SELECT name, department, salary
FROM employees
WHERE salary >= 350000 OR (department = 'Future Gadget Lab' AND salary >= 300000);

Notes

The precedence of SQL logical operators is NOTANDOR. For example, A OR B AND C is evaluated as A OR (B AND C). To avoid unintended results, always use parentheses for complex conditions.

When listing multiple OR conditions, using the IN clause is more readable. salary = 260000 OR salary = 350000 is equivalent to salary IN (260000, 350000).

AND, OR, and NOT can also be used in HAVING clauses, not just WHERE.

If you find any errors or copyright issues, please .