How to combine conditions with AND OR NOT

· Category: SQL & Databases

Short answer

Use AND to require multiple conditions, OR to accept any condition, and NOT to negate a condition. Parentheses control evaluation order.

Steps

  1. Both conditions: SELECT * FROM products WHERE price > 100 AND stock > 0;
  2. Either condition: SELECT * FROM products WHERE category = 'Electronics' OR category = 'Books';
  3. Negation: SELECT * FROM products WHERE NOT discontinued;
  4. Complex logic: SELECT * FROM orders WHERE (status = 'shipped' OR status = 'delivered') AND order_date > '2025-01-01';
  5. Use IN as a cleaner alternative to multiple OR equalities.

Tips

  • AND has higher precedence than OR; use parentheses to make intent explicit.
  • NOT IN with a subquery that returns NULL yields no rows, which is a common trap.

Common issues

  • Missing parentheses around OR conditions combined with AND causes logic errors.
  • NOT with NULL evaluates to NULL, not true.r