Comparison Operators
Introduction to SQL
Database
SQL Basics
Creating a Database
Using a Database
Creating a Table
Common Data Types
Inserting Data
Selecting Data
WHERE Clause
Comparison Operators
AND, OR, and NOT
ORDER BY Clause
LIMIT Clause
DISTINCT
LIKE Operator
IN, BETWEEN, and IS NULL
Updating Data
Deleting Data
ALTER TABLE
DROP TABLE
PRIMARY KEY and NOT NULL
FOREIGN KEY and Relationships
Aggregate Functions
GROUP BY Clause
HAVING Clause
JOIN Basics
INNER JOIN
LEFT JOIN
Aliases
Subqueries
Indexes
User Permissions and Security
Common Mistakes Beginners Make
Practice Ideas
Comparison Operators
Comparison Operators
Definition: Comparison operators are symbols used in SQL filters to determine the relationship between two values. They are the building blocks of the WHERE clause, allowing the database to evaluate whether a record should be included in your results.
Usage: Beginner SQL tutorials introduce these early because they allow you to move beyond simply viewing data to actually asking specific questions, such as "Which students are from a specific city?" or "Which products are out of stock?"
Common Comparison Operators
These operators are used to compare a column's value against a specific criteria:
| Operator | Description | Example Usage |
|---|---|---|
= |
Equal to | Finds an exact match for text or numbers. |
> |
Greater than | Finds values strictly higher than the target. |
< |
Less than | Finds values strictly lower than the target. |
>= |
Greater than or equal to | Includes the target value and anything higher. |
<= |
Less than or equal to | Includes the target value and anything lower. |
<> or != |
Not equal to | Excludes a specific value from the results. |
Practical Example
To retrieve all student records for those living specifically in Bengaluru, you use the Equal to operator:
SELECT * FROM students WHERE city = 'Bengaluru';
Explanation
- Filtering by Text: When comparing text (like
'Bengaluru'), always wrap the value in single quotes. - Filtering by Number: When comparing numbers (like
age > 18), do not use quotes. - Logical Evaluation: For every row in the
studentstable, the database asks: "Is the city equal to Bengaluru?" If the answer is True, the row is displayed. If False, the row is hidden.
Key Notes
- Standardization: While
!=is widely accepted in modern SQL,<>is the official ISO standard for "not equal to." Both usually work, but<>is more portable across different database types. - Null Values: Operators like
=or!=do not work onNULL(empty) values. To find empty data, you must useIS NULLorIS NOT NULL. - Case Sensitivity: Depending on the database settings (Collation),
'bengaluru'might be treated differently than'Bengaluru'. It is best practice to match the case exactly as it is stored.
🏋️ Test Yourself With Exercises
Take our quiz on Comparison Operators to test your knowledge.
Browse Quizzes »