Comparison operators #
The condition in a WHERE clause is usually built with a comparison operator. SQL supports the following:
| Operator | Meaning |
|---|---|
= |
equal to |
<> or != |
not equal to |
< |
less than |
> |
greater than |
<= |
less than or equal to |
>= |
greater than or equal to |
Example #
SELECT
*
FROM
products
WHERE
price > 30;
Result:
+----+-------------------+-------------+-------+
| id | name | category | price |
+----+-------------------+-------------+-------+
| 2 | Bluetooth Speaker | Electronics | 45.5 |
| 4 | Running Shoes | Fitness | 60.0 |
| 8 | Backpack | Office | 40.0 |
+----+-------------------+-------------+-------+
Only the products with a price greater than 30 are kept. You can use any of the comparison operators above the same way, for example:
SELECT
*
FROM
products
WHERE
category <> 'Office';
keeps every row whose category is not 'Office'.
Exercise #
Write a query that retrieves all columns from the products table, keeping only the rows where price is greater than 30.
- The result has a
pricecolumn. - The result contains exactly the rows where
priceis greater than30, and no others.
Solution
SELECT
*
FROM
products
WHERE
price > 30;
Basic Data Retrieval
Filtering Data
Aggregating Data
Joining Tables
Creating and Managing Tables
Transactions and Data Integrity