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:
namepriceidis_saleinventorycreated_at
TV2001false02021-08-20 00:49:58.021274-04
DVD Players802false02021-08-20 00:49:58.021274-04
remote103false02021-08-20 00:49:58.021274-04
microphone305false02021-08-20 00:49:58.021274-04
Car407false02021-08-20 00:49:58.021274-04
pencil28false02021-08-20 00:49:58.021274-04
pencil sharpener49true02021-08-20 00:49:58.021274-04
keyboard2810false502021-08-20 00:50:48.457985-04
soda211true102021-08-20 23:01:37.283024-04
pizza1312true222021-08-20 23:01:37.283024-04
toothbrush213true82021-08-20 23:01:37.283024-04
toilet paper414false1002021-08-20 23:02:37.786025-04
xbox38015true452021-08-20 23:04:23.608326-04
Operator quick reference
OperatorMeaningExample
=Equal toWHERE price = 200
<>Not equal (SQL standard)WHERE inventory <> 0
!=Not equal (also supported)WHERE inventory != 0
>Greater thanWHERE price > 50
>=Greater than or equal toWHERE price >= 80
<Less thanWHERE price < 80
<=Less than or equal toWHERE price <= 80
ANDLogical AND (both conditions true)WHERE inventory > 0 AND price > 20
ORLogical OR (either condition true)WHERE price > 100 OR price < 20
Equality (=)
  • Use = to match exact values.
-- Find products priced at 200
SELECT * FROM products WHERE price = 200;
Possible result:
name | price | id | is_sale | inventory | created_at
TV   | 200   | 1  | false   | 0         | 2021-08-20 00:49:58.021274-04
Greater than / Less than (> / < / >= / <=)
  • Use >, <, >=, <= just like in most programming languages to compare numeric or date values.
-- Prices strictly greater than 50
SELECT * FROM products WHERE price > 50;
Example result:
name        | price | id | is_sale | inventory | created_at
TV          | 200   | 1  | false   | 0         | ...
DVD Players | 80    | 2  | false   | 0         | ...
xbox        | 380   | 15 | true    | 45        | ...
Additional examples:
-- Prices greater than or equal to 80 (includes 80)
SELECT * FROM products WHERE price >= 80;

-- Prices less than 80 (strictly less than 80)
SELECT * FROM products WHERE price < 80;

-- Prices less than or equal to 80 (includes 80)
SELECT * FROM products WHERE price <= 80;
Not equal (<> and !=)
  • SQL supports two common not-equal syntaxes: <> (SQL standard) and != (supported by many engines such as PostgreSQL).
-- Using !=
SELECT * FROM products WHERE inventory != 0;

-- Using <> (SQL standard)
SELECT * FROM products WHERE inventory <> 0;
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):
-- Items with inventory greater than 0 AND price greater than 20
SELECT * FROM products WHERE inventory > 0 AND price > 20;
Example (OR):
-- Items with price greater than 100 OR less than 20
SELECT * FROM products WHERE price > 100 OR price < 20;
Precedence and grouping:
-- Without parentheses AND has higher precedence than OR in SQL,
-- but it's clearer to group conditions explicitly if logic is complex:
SELECT * FROM products WHERE (price > 100 AND inventory > 0) OR is_sale = true;
Common mistake: missing WHERE
  • Forgetting the WHERE keyword is a frequent source of syntax errors.
-- Incorrect: missing WHERE
SELECT * FROM products inventory > 0 AND price > 20;
Typical error (psql example):
ERROR:  syntax error at or near "inventory"
LINE 1: SELECT * FROM products inventory > 0 AND price > 20;
                               ^
SQL state: 42601
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