- SQL Basics
- SQL Introduction
- SQL Syntax
- SQL Sample Database
- SQL SELECT
- SQL WHERE
- SQL ORDER BY
- SQL DISTINCT
- SQL LIMIT
- SQL FETCH
- SQL AND
- SQL OR
- SQL BETWEEN
- SQL IN
- SQL LIKE
- SQL IS NULL
- SQL Comparison Operators
- SQL Logical Operators
- SQL Alias
- SQL CASE
- Joins and Subqueries
- SQL INNER JOIN
- SQL LEFT JOIN
- SQL RIGHT JOIN
- SQL FULL OUTER JOIN
- SQL SELF JOIN
- SQL CROSS JOIN
- SQL Subquery
- SQL Correlated Subquery
- SQL UNION
- SQL INTERSECT
- SQL EXCEPT
- Aggregate Functions
- SQL AVG
- SQL COUNT
- SQL MAX
- SQL MIN
- SQL SUM
- SQL GROUP BY
- SQL HAVING
- SQL ROLLUP
- SQL CUBE
- SQL GROUPING SETS
- Database Management
- SQL CREATE DATABASE
- SQL ALTER DATABASE
- SQL DROP DATABASE
- SQL BACKUP DATABASE
- SQL SHOW DATABASES
- SQL SELECT DATABASE
- Table Management
- SQL CREATE TABLE
- SQL ALTER TABLE
- SQL ADD COLUMN
- SQL DROP COLUMN
- SQL DROP TABLE
- SQL TRUNCATE TABLE
- SQL SHOW TABLES
- SQL RENAME TABLE
- SQL Constraints
- SQL Primary Key
- SQL Foreign Key
- SQL UNIQUE Constraint
- SQL CHECK Constraint
- SQL NOT NULL Constraint
SQL Comparison Operators
SQL comparison operators are the fundamental tools used to filter data within a WHERE clause. They allow you to define specific criteria by comparing column values against constants, expressions, or other columns. When a comparison is made, SQL evaluates the condition and returns one of three states: TRUE, FALSE, or UNKNOWN (which typically happens when dealing with NULL values).
Understanding these operators is essential for any developer looking to write efficient queries for reporting, data analysis, or application logic.
List of SQL Comparison Operators
!= and <> represent "Not Equal," <> is the official ISO standard for SQL. Most modern databases support both, but using <> ensures better portability across different database systems like PostgreSQL, MySQL, and SQL Server.
Key Features of Comparison Operators
1. Equality Check (=)
The = operator is used to find rows where the column data matches the provided value exactly. This is frequently used for IDs, status codes, and specific names.
SELECT name, salary
FROM employees
WHERE status = 'Active';
- In this example, only employees whose status is exactly 'Active' are returned.
= NULL to find missing values. In SQL, NULL represents an unknown value, so NULL = NULL is actually False. You must use IS NULL instead.
2. Not Equal (<> or !=)
This operator filters out specific values. It is highly useful when you want to see everything except a particular group.
SELECT product_name, category
FROM products
WHERE category <> 'Discontinued';
- This query pulls all products that are currently in stock or pending, excluding any marked as 'Discontinued'.
3. Greater Than (>) and Less Than (<)
These are used for numerical comparisons and chronological date filtering.
SELECT order_id, order_date
FROM orders
WHERE order_date > '2023-12-31';
- This retrieves all orders placed after the year 2023. Note that strings and dates must be wrapped in single quotes.
4. Greater Than or Equal To (>=) and Less Than or Equal To (<=)
These operators are "inclusive," meaning they include the value you are comparing against.
SELECT student_name, grade
FROM exams
WHERE grade >= 70;
- A student with a grade of exactly 70 will be included in these results.
price >= 10 AND price <= 20), consider using the BETWEEN operator for better readability. SQL engines optimize both similarly, but BETWEEN is often easier for other developers to read.
Examples of Comparison Operators
1. Practical Data Filtering
In a real-world e-commerce scenario, you might want to find premium users who are also under a certain age for a targeted marketing campaign.
SELECT email, age
FROM users
WHERE account_type = 'Premium' AND age < 25;
2. Managing Inventory with Multi-Condition Filters
Combining operators helps you pinpoint specific data sets, such as low-stock items that are high-priority.
SELECT product_name, stock_count
FROM inventory
WHERE stock_count <= 10 AND priority_level > 5;
name > 'M'), the result depends on the "Collation" (alphabetical sorting rules) of your database. Some databases are case-sensitive, while others are not.
3. Using NOT with Comparison Operators
The NOT keyword can be used to reverse any comparison logic, which is sometimes more readable than using the negative operator directly.
SELECT item_name, price
FROM menu
WHERE NOT price > 20.00;
- This effectively performs the same action as
price <= 20.00.
4. Handling NULL Values Correctly
As mentioned earlier, NULL is tricky. If a column allows NULL, those rows will be ignored by almost all standard comparison operators.
SELECT name
FROM employees
WHERE manager_id IS NULL;
- Pro Tip: If you want to find employees who do not have a manager, you cannot use
manager_id = NULL. You must use theIS NULLsyntax.
Summary
SQL comparison operators are the primary way to define logic in your queries. By mastering =, <>, >, <, >=, and <=, you gain full control over your data retrieval. Always remember that NULL requires special handling with IS NULL, and strive to use the standard <> for inequality to keep your code clean and professional.