Comparison Operators
Comparison operators are the building blocks of filtering logic.
They let you ask questions like "equal to," "greater than," or "not equal."
Core Concepts
flowchart LR
A[Column value] --> B[Comparison operator]
B --> C[Literal or expression]
C --> D[True/false per row]
| Operator | Meaning | Example |
|---|---|---|
= | Equal to | score = 100 |
!= or <> | Not equal to | status != 'closed' |
> | Greater than | price > 50 |
< | Less than | age < 18 |
>= | Greater than or equal | qty >= 1 |
<= | Less than or equal | qty <= 10 |
Code Examples
-- Setup comparison demo table.
CREATE TABLE orders_cmp (
order_id INTEGER PRIMARY KEY,
total REAL NOT NULL,
status TEXT NOT NULL
);
INSERT INTO orders_cmp (total, status)
VALUES (19.99, 'new'), (120.00, 'paid'), (75.50, 'new');
| Expected output |
|---|
| Table created and three rows inserted. |
-- Compare totals and status.
SELECT order_id, total, status
FROM orders_cmp
WHERE total >= 75.50 AND status != 'cancelled';
| order_id | total | status |
|---|---|---|
| 2 | 120.00 | paid |
| 3 | 75.50 | new |
SQLite-Specific Nuances
SQLite Nuance
SQLite supports both != and <> for "not equal".
Common Pitfalls / Best Practices
Pitfall
Using = NULL or != NULL for null checks.
Use IS NULL or IS NOT NULL instead.
Best Practice
Keep comparisons type-consistent where possible (number-to-number, text-to-text) for clearer intent.
Quick Challenge
Return rows from orders_cmp where total < 100.
View Solution
SELECT order_id, total, status
FROM orders_cmp
WHERE total < 100;
| order_id | total | status |
|---|---|---|
| 1 | 19.99 | new |
| 3 | 75.50 | new |