Comparison Operators
In a typical e-commerce scenario, users would generally filter the products with good ratings, or want to purchase the products of a certain brand or of a certain price. Let's see how comparison operators are used to filter such kind of data using the following database.
Database The database contains a
product
table that stores the data of products like name, category, price, brand and rating.
You can check the schema and data ofproduct
table in the code playground.Comparison Operators
Operator | Description |
---|---|
= | Equal to |
<> | Not equal to |
< | Less than |
<= | Less than or equal to |
> | Greater than |
>= | Greater than or equal to |
Examples
- Get all the details of the products whosecategoryis "Food" from theproducttable.
SQL
Output
name | category | price | brand | rating |
---|---|---|---|---|
Chocolate Cake | Food | 25 | Britannia | 3.7 |
Strawberry Cake | Food | 60 | Cadbury | 4.1 |
Chocolate Cake | Food | 60 | Cadbury | 2.5 |
... | ... | ... | ... | ... |
- Get all the details of the products that does not belong toFoodcategory from theproducttable.
SQL
Output
name | category | price | brand | rating |
---|---|---|---|---|
Blue Shirt | Clothing | 750 | Denim | 3.8 |
Blue Jeans | Clothing | 800 | Puma | 3.6 |
Black Jeans | Clothing | 750 | Denim | 4.5 |
... | ... | ... | ... | ... |
Similarly, we can use other comparison operators like greater than (>), greater than or equal to (>=), less than (<), less than or equal to (<=) to filter the data as per the requirement.
Try it Yourself!
Put your learning into practice and try fetching the products based on the conditions mentioned.
Write a query for each of the below conditions.
- ratinggreater than 4.5
- priceis less than or equal to 1000
- brandis "Puma"
- that does not belong to "Gadgets" category