Comparison Operators
Comparison operators in SQL are used to compare two values in a WHERE clause or in a join condition. They are essential for filtering data and creating conditions in your queries.
List of Comparison Operators
Operator | Description |
---|---|
= | Equal to |
<> | Not equal to |
!= | Not equal to (alternative) |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Examples
Equal to (=)
SELECT * FROM products WHERE category = 'Electronics';
This query selects all products in the 'Electronics' category.
Not equal to (<> or !=)
SELECT * FROM orders WHERE status <> 'Shipped';
This query selects all orders that have not been shipped.
Greater than (>)
SELECT * FROM employees WHERE salary > 50000;
This query selects all employees with a salary greater than 50,000.
Less than (<)
SELECT * FROM inventory WHERE quantity < 10;
This query selects all inventory items with a quantity less than 10.
Greater than or equal to (>=)
SELECT * FROM students WHERE grade >= 70;
This query selects all students with a grade of 70 or higher.
Less than or equal to (<=)
SELECT * FROM products WHERE price <= 100;
This query selects all products with a price of 100 or less.
Remember, these operators can be used in various parts of your SQL queries, not just in the WHERE clause. They're also commonly used in JOIN conditions and HAVING clauses.