Comparison Operators (=, !=, >, <)
Comparison operators form the backbone of every WHERE clause. They compare a column value to a given value and return TRUE or FALSE. Understanding each operator and when to use it is foundational to writing any meaningful SQL query.
All Comparison Operators
- = Equal: WHERE status = 'active'
- != or <> Not equal: WHERE category != 'Furniture'
- < Less than: WHERE price < 100
- > Greater than: WHERE price > 500
- <= Less than or equal: WHERE age <= 18
- >= Greater than or equal: WHERE stock >= 50
- NULL comparisons NEVER use = — use IS NULL / IS NOT NULL
Comparison Operators on ecommerce_db
-- Equal: products priced exactly 599.99
SELECT name, price FROM products WHERE price = 599.99;
-- Not equal: all products except Laptops
SELECT name FROM products WHERE name != 'Laptop';
-- OR: WHERE name <> 'Laptop' (both work)
-- Greater than: expensive products
SELECT name, price FROM products WHERE price > 500;
-- Result: Laptop (999.99), Phone (599.99)
-- Less than or equal: affordable products
SELECT name, price FROM products WHERE price <= 100;
-- Result: Headphones (89.99)
-- Compare dates: orders from 2024
SELECT id, status FROM orders
WHERE created_at >= '2024-01-01';
-- Greater than or equal stock check
SELECT name, stock FROM products WHERE stock >= 100;
-- Result: Phone (120), Headphones (200)Quick Quiz
Tip
Tip
Practice Comparison Operators in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
SQL for structured data + ACID. MongoDB for flexible schemas. Redis for caching + sessions.
Common Mistake
Warning
A common mistake with Comparison Operators is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready sql code.
Practice Task
Note
Practice Task — (1) Write a working example of Comparison Operators from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Key Takeaways
- Comparison operators form the backbone of every WHERE clause.
- = Equal: WHERE status = 'active'
- != or <> Not equal: WHERE category != 'Furniture'
- < Less than: WHERE price < 100