SQL Comparison Operators
beginnerfiltering
2 min read
What are Comparison Operators?
Comparison operators compare two values and return a boolean result. They are the building blocks of WHERE clause conditions.
Operators
| Operator | Meaning |
|---|---|
= | Equal to |
<> or != | Not equal to |
< | Less than |
> | Greater than |
<= | Less than or equal to |
>= | Greater than or equal to |
Syntax
SELECT * FROM table_name WHERE column operator value;When to Use
- Any time you need to compare values in a filter condition
- Checking thresholds (price > 100)
- Exact matching (status = 'active')
Key Points
- Two Not-Equal Operators — Both
<>(SQL standard) and!=(common extension) work in PostgreSQL. - String Comparison — Strings are compared lexicographically based on the collation.
- NULL Behavior — Any comparison with NULL returns unknown. Use
IS NULLinstead. - Type Matching — Compare values of the same type. PostgreSQL performs implicit casts in some cases, but explicit casting is safer.
- Date Comparison — Dates and timestamps can be compared with these operators naturally.
Guided Practice
Solve the challenge below. Use hints when stuck and check your answer for instant feedback.
Practice challengeGuided learning mode
SQL Comparison Operators Challenge
Write a query that find products priced above 50.
Expected result
All products with a price greater than 50.
Hidden checks
- Returned rows and values
- Output columns and result shape
- Final database state after the query runs
Lesson guidance
What are Comparison Operators?
Initializing database...Each run starts from fresh sample data.
More Examples
Not equal comparison
Find students whose grade is not C.
Initializing database...Each run starts from fresh sample data.
Frequently Asked Questions
What is the difference between <> and != in SQL?
Both mean 'not equal.' <> is the SQL standard operator, while != is a widely supported alternative. In PostgreSQL they are identical.
Can I compare dates with > and < operators?
Yes. Dates and timestamps are naturally orderable. WHERE order_date > '2024-01-01' returns orders after January 1, 2024.