CodingNic

Querying Data

AND, OR, and NOT

Querying Data 10 min read

AND, OR, and NOT

Objectives

By the end of this lesson, you should be able to:

  • Combine multiple conditions with AND and OR
  • Negate a condition with NOT
  • Explain how PostgreSQL evaluates AND and OR together

💡 Why this matters: Real filtering conditions are rarely just one comparison. “Engineering employees earning over 80000” needs both conditions true at once, “Sales or Marketing” needs either one.

⚠️ A note on verification: every statement and result in this lesson was run against a real, live PostgreSQL 18 database, using the employees table from the last lesson.

AND: Every Condition Must Be True

sql
SELECT first_name, department, salary FROM employees
WHERE department = 'Engineering' AND salary > 80000;
text
 first_name | department  | salary
------------+-------------+---------
 Priya      | Engineering | 89000.00
 Taylor     | Engineering | 95000.00

Both conditions have to be true for a row to appear, Erin is in Engineering but earns under 80000, so she’s excluded. AND can chain more than two conditions, every single one has to hold.

OR: At Least One Condition Must Be True

sql
SELECT first_name, department FROM employees
WHERE department = 'Sales' OR department = 'Marketing';
text
 first_name | department
------------+-------------
 Jordan     | Sales
 Maya       | Marketing
 Sam        | Sales
 Alexis     | Marketing

Only one of the two conditions needs to hold for a row to appear. This particular case, checking one column against several possible values, is common enough that it has its own shorthand, IN, covered in the next lesson.

NOT: Negating a Condition

sql
SELECT first_name, department FROM employees WHERE NOT department = 'Engineering';
text
 first_name | department
------------+-------------
 Jordan     | Sales
 Maya       | Marketing
 Sam        | Sales
 Alexis     | Marketing

NOT department = 'Engineering' and department != 'Engineering' (last lesson) return the same result here, NOT flips whatever condition follows it. NOT is more useful once conditions get more complex, NOT (department = 'Engineering' AND salary > 80000) is a lot clearer than trying to write that negation with comparison operators alone.

Combining AND and OR

AND binds tighter than OR, the same way multiplication binds tighter than addition in arithmetic. Use parentheses whenever a query mixes both, to make the actual grouping explicit rather than relying on the default:

sql
SELECT first_name, department, salary FROM employees
WHERE (department = 'Sales' OR department = 'Marketing') AND salary > 65000;

Without the parentheses, this would be read as department = 'Sales' OR (department = 'Marketing' AND salary > 65000), a genuinely different condition. When in doubt, add the parentheses, they cost nothing and remove any ambiguity, for you and for anyone reading the query later.

Try It

  1. Write a query for every employee in Marketing earning more than 68000.
  2. Write a query for every employee in either Engineering or Sales.
  3. Write a query using NOT for every employee not in Marketing.
  4. Write a query for every employee in Engineering or Sales, earning more than 70000, using parentheses to make the grouping explicit.

Recap

  • AND requires every condition to be true, OR requires at least one.
  • NOT negates whatever condition follows it.
  • AND binds tighter than OR, use parentheses whenever a query mixes both, to make the grouping explicit.

Next lesson: BETWEEN and IN, shorthand for two very common filtering patterns.