Skip to main content
In this lesson we cover common SQL comparison and logical operators. Up to now we’ve mainly used equality in WHERE clauses; here we’ll expand to greater-than, less-than, not-equal, and boolean combinations (AND / OR) with clear examples and best practices. To follow the examples, assume the products table contains rows like the following: Operator quick reference Equality (=)
  • Use = to match exact values.
Possible result:
Greater than / Less than (> / < / >= / <=)
  • Use >, <, >=, <= just like in most programming languages to compare numeric or date values.
Example result:
Additional examples:
Not equal (<> and !=)
  • SQL supports two common not-equal syntaxes: <> (SQL standard) and != (supported by many engines such as PostgreSQL).
Either query returns rows where inventory is not zero (i.e., items in stock).
Use <> when you want to follow SQL standard syntax. Many databases accept both <> and !=; pick one consistent with your team’s style guide or your DBMS documentation.
Combining conditions: AND / OR
  • Use AND to require multiple conditions and OR to return rows that satisfy at least one condition.
  • Use parentheses to control precedence when mixing AND and OR.
Example (AND):
Example (OR):
Precedence and grouping:
Common mistake: missing WHERE
  • Forgetting the WHERE keyword is a frequent source of syntax errors.
Typical error (psql example):
Always include WHERE after FROM (and after any JOIN clauses) when filtering rows. When queries become complex, format and indent conditions to make missing keywords obvious.
Summary and best practices
  • Use =, >, <, >=, <= for comparisons.
  • Prefer <> for not-equal to follow SQL standard; != is often supported but be consistent.
  • Combine conditions with AND and OR; use parentheses to group logic explicitly.
  • Place WHERE after FROM (and after JOINs) — omitting it causes syntax errors.
  • For readability and maintainability, format multi-condition WHERE clauses on multiple lines and consider adding comments for complex boolean logic.
Links and references

Watch Video