Topic 15 of 52
=, !=, >, <
Overview
SQL comparison operators are used in WHERE clauses to filter rows based on column values. They form the building blocks of all data filtering logic.
Syntax
sql
-- Equality
WHERE status = 'active'
WHERE name = 'Priya'
-- Inequality
WHERE status != 'cancelled' -- standard SQL
WHERE status <> 'cancelled' -- alternative (SQL standard)
-- Greater/less than
WHERE price > 1000
WHERE price >= 1000 -- greater than or equal
WHERE stock < 10
WHERE stock <= 0
-- Combining comparisons
WHERE price >= 100 AND price <= 5000 -- same as BETWEEN 100 AND 5000
WHERE created_at > '2025-01-01'
WHERE salary >= 50000 AND salary <= 100000
-- Comparing columns to each other
WHERE min_price <= max_price -- ensure valid range
WHERE updated_at > created_at -- was record actually updated?Common Pitfalls
- String comparisons are case-sensitive in most databases — 'Active' != 'active'. Use LOWER(column) = 'active' for case-insensitive matching.
- Date comparisons can be tricky — '2025-06-13' < '2025-06-13 10:00:00' is true because dates are cast to midnight.
- Interview tip: != and <> are equivalent in SQL — both mean 'not equal'. Use whichever your team prefers for consistency.
Real-World Example
Inventory alert system using comparison operators:
example
sql
-- Products needing restocking
SELECT
p.id,
p.name,
p.sku,
p.stock AS current_stock,
p.reorder_point,
p.stock - p.reorder_point AS units_below_threshold
FROM products p
WHERE
p.stock <= p.reorder_point -- below or at reorder threshold
AND p.is_active = TRUE
AND p.discontinued_at IS NULL
ORDER BY (p.stock - p.reorder_point) ASC; -- most critical first
-- Price anomalies (cost higher than selling price)
SELECT id, name, cost_price, selling_price,
selling_price - cost_price AS margin
FROM products
WHERE selling_price < cost_price -- negative margin!
OR selling_price = 0; -- free? probably an error