SQL Basics
- What is SQL?
- Data Definition Language
- Data Manipulation Language
- Data Query Language
- Data Control Language
- Transaction Control Language
- Tables and Schemas
- Data Types
- Constraints
Querying and Filtering
💡 Mastering SQL Comparison Operators: =, !=, >, <, BETWEEN & More Explained Clearly
When working with data in SQL, comparison operators are the building blocks of making decisions and filtering results. They allow you to compare column values against constants, expressions, or other columns—essential for writing meaningful queries.
Whether you’re filtering sales records, user ages, or transaction dates, these operators let you ask precise questions and get relevant answers from your database.
In this guide, we’ll explore the most commonly used SQL comparison operators, how they work, and show you clear examples you can follow—even as a beginner.
🚀 What Are Comparison Operators in SQL?
Comparison operators are used in SQL to compare two values. The result of this comparison is either TRUE or FALSE, which determines whether a particular row should be included in the result set.
These operators are mainly used in:
WHERE
clauses (to filter data)JOIN
conditions (to connect tables)CASE
statements (to perform logic)HAVING
clauses (for filtering grouped data)
🔧 Basic SQL Comparison Operators
Let’s take a look at the most frequently used SQL comparison operators:
Operator | Description | Example |
---|---|---|
= | Equal to | age = 30 |
!= or <> | Not equal to | status != 'active' |
> | Greater than | salary > 50000 |
< | Less than | quantity < 10 |
>= | Greater than or equal to | score >= 90 |
<= | Less than or equal to | price <= 1000 |
BETWEEN | Within a specified range | age BETWEEN 20 AND 30 |
NOT BETWEEN | Outside a specified range | price NOT BETWEEN 100 AND 500 |
🔍 Using the =
Operator (Equal To)
The simplest and most used comparison operator.
SELECT *FROM studentsWHERE grade = 'A';
This returns all students who received an A grade.
🔁 Using the !=
or <>
Operator (Not Equal To)
Both symbols work similarly, though <>
is ANSI SQL standard and more widely accepted.
SELECT *FROM usersWHERE status != 'inactive';
This filters out all inactive users.
🔼 Using >
and <
(Greater Than, Less Than)
You can compare numbers, dates, or even alphabetical values depending on the column type.
Example 1: Numeric
SELECT *FROM productsWHERE price > 100;
Example 2: Dates
SELECT *FROM ordersWHERE order_date < '2023-01-01';
This gets orders placed before January 2023.
🔁 Using >=
and <=
(Greater Than or Equal, Less Than or Equal)
These are often used to include boundary values in your filtering.
SELECT *FROM examsWHERE score >= 75;
Shows students who passed the exam with at least 75 marks.
🎯 The Powerful BETWEEN
Operator
The BETWEEN
operator checks if a value lies within a range of values, inclusive of the boundary numbers.
SELECT *FROM employeesWHERE age BETWEEN 30 AND 40;
Returns employees aged between 30 and 40.
You can also use BETWEEN
with dates:
SELECT *FROM bookingsWHERE check_in BETWEEN '2024-01-01' AND '2024-12-31';
❗ Using NOT BETWEEN
To filter out values outside a range:
SELECT *FROM booksWHERE price NOT BETWEEN 10 AND 20;
This fetches all books that cost less than 10 or more than 20.
🔗 Combining Comparison Operators
You can combine multiple comparisons using AND
, OR
, and NOT
.
Example with AND
SELECT *FROM employeesWHERE department = 'Sales' AND salary > 50000;
Example with OR
SELECT *FROM productsWHERE category = 'Electronics' OR price < 100;
Example with NOT
SELECT *FROM usersWHERE NOT age BETWEEN 18 AND 25;
🧠 Real-Life Example: Applying Multiple Comparison Operators
Scenario: A company wants to find customers who:
- Placed an order in 2024
- Ordered more than $500 worth
- Live outside of California
SELECT *FROM customersWHERE state != 'CA' AND total_spent > 500 AND order_date BETWEEN '2024-01-01' AND '2024-12-31';
This is a great use of !=
, >
, and BETWEEN
in a single query.
🧩 Comparison Operators with Strings
SQL also allows comparisons between string values. Keep in mind, strings are compared alphabetically (lexicographically).
SELECT *FROM citiesWHERE city_name > 'M';
This returns cities that come after M alphabetically.
📝 Notes on NULLs and Comparisons
SQL uses a special keyword NULL
to represent missing values. Comparing NULL using =
or !=
won’t work.
-- WrongWHERE email = NULL
-- CorrectWHERE email IS NULL
🧼 Best Practices
- Always quote string values with
'single quotes'
. - Avoid
!= NULL
— useIS NOT NULL
. - Use parentheses for clarity when combining multiple conditions.
- Know your data types—comparing numbers and strings can lead to unexpected results.
- Use BETWEEN when you’re working with ranges—it makes your query more readable and efficient.
❓ Why Should Beginners Learn These Operators First?
Comparison operators are used in nearly every SQL query beyond the most basic ones. Whether you’re filtering data, analyzing performance, or extracting trends, you’ll need these operators.
Understanding them helps you:
- Build complex queries
- Make decisions with
CASE
orIF
- Perform better data analysis
- Combine conditions logically
🏁 Conclusion
SQL comparison operators like =
, !=
, >
, <
, BETWEEN
, and others form the foundation of data filtering. They’re not just tools—they are your language for asking smart questions of your data.
The more you use them, the more natural they become. Try them out in your own database projects or with practice tools like SQL Fiddle or SQLite online. And remember, clear logic makes for clean data!