Comparison and logical operators are the building blocks of every WHERE clause you'll write. Comparison operators test a single relationship between a column and a value; logical operators combine several of those tests into one overall condition.
Comparison Operators
= -- equal to
!= -- not equal to (also written <>)
<> -- not equal to (standard SQL form)
< -- less than
> -- greater than
<= -- less than or equal to
>= -- greater than or equal to
SELECT name, salary
FROM employees
WHERE salary <> 72000;
Result:
name | salary
--------------|--------
Alice Chen | 95000.00
Priya Nair | 88000.00
Logical Operators: AND, OR, NOT
AND requires every condition to be true. OR requires at least one to be true. NOT inverts a condition entirely.
-- AND: both conditions must hold
SELECT name FROM employees
WHERE department = 'Engineering' AND salary > 90000;
-- OR: either condition is enough
SELECT name FROM employees
WHERE department = 'Sales' OR department = 'Marketing';
-- NOT: the opposite of the condition
SELECT name FROM employees
WHERE NOT department = 'Engineering';
Operator Precedence and Parentheses
Just like arithmetic, logical operators have a precedence order: NOT is evaluated first, then AND, then OR. This means mixing AND and OR without parentheses can silently produce a different result than you intended:
-- Without parentheses — AND binds tighter than OR here,
-- so this actually means: department = 'Sales'
-- OR (department = 'Engineering' AND salary > 90000)
SELECT name, department, salary FROM employees
WHERE department = 'Sales' OR department = 'Engineering' AND salary > 90000;
-- With parentheses — now it's unambiguous:
-- either department, but only if salary also clears the bar
SELECT name, department, salary FROM employees
WHERE (department = 'Sales' OR department = 'Engineering')
AND salary > 90000;
These two queries can return genuinely different rows depending on the data. The safest habit is to add parentheses any time you mix AND and OR in the same condition — it costs nothing and removes all ambiguity about what the query actually filters.
Putting It Together
Comparison operators answer "is this true for one column?" and logical operators answer "how do several of those answers combine?" Together they let a single WHERE clause express fairly complex business rules — once you're comfortable chaining a few conditions, shortcuts like BETWEEN and IN will feel like natural extensions rather than new concepts.
