Skip to main content

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]
OperatorMeaningExample
=Equal toscore = 100
!= or <>Not equal tostatus != 'closed'
>Greater thanprice > 50
<Less thanage < 18
>=Greater than or equalqty >= 1
<=Less than or equalqty <= 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_idtotalstatus
2120.00paid
375.50new

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_idtotalstatus
119.99new
375.50new