Comparison Operators Cheat Sheet > Querying with SQL

 

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 of
product
table in the code playground.

Comparison Operators

OperatorDescription
=Equal to
<>Not equal to
<Less than
<=Less than or equal to
>Greater than
>=Greater than or equal to

Examples

  1. Get all the details of the products whose
    category
    is "Food" from the
    product
    table.
SELECT
*
FROM
product
WHERE
category = "Food";
SQL
Output
namecategorypricebrandrating
Chocolate CakeFood25Britannia3.7
Strawberry CakeFood60Cadbury4.1
Chocolate CakeFood60Cadbury2.5
...............
  1. Get all the details of the products that does not belong to
    Food
    category from the
    product
    table.
SELECT
*
FROM
product
WHERE
category <> "Food";
SQL
Output
namecategorypricebrandrating
Blue ShirtClothing750Denim3.8
Blue JeansClothing800Puma3.6
Black JeansClothing750Denim4.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.

  1. rating
    greater than 4.5
  2. price
    is less than or equal to 1000
  3. brand
    is "Puma"
  4. that does not belong to "Gadgets" 
    category

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

Contact Form