1757 Recyclable and Low-Fat Products

Problem Statement

Table Products

Column NameType

product_id

int

low_fats

enum

recyclable

enum

  • product_id is the primary key (column with unique values) for this table.

  • low_fats is an ENUM (category) of type ('Y', 'N') where 'Y' means this product is low fat and 'N' means it is not.

  • recyclable is an ENUM (category) of types ('Y', 'N') where 'Y' means this product is recyclable and 'N' means it is not.

Instructions

  • Write an SQL query to find the product_id of the products that are both low fat and recyclable.

  • Return the result table in any order.

  • The query result format is in the following example.

For the whole problem statement, please refer here.

Plans

  • We need to find the product_id of the products that are both low fat and recyclable.

  • We can achieve this by filtering the rows in the Products table where low_fats = 'Y' and recyclable = 'Y'.

  • We will use the SELECT statement to retrieve the product_id column from the Products table.

  • We will use the WHERE clause to filter the rows where low_fats = 'Y' and recyclable = 'Y.

  • Finally, we will return the product_id of the products that are both low fat and recyclable.

Solution

SELECT product_id
FROM Products
WHERE low_fats = 'Y' AND recyclable = 'Y';

Explanation

  1. SELECT product_id:

    • This part of the query specifies that we want to retrieve the product_id column from the table. The product_id uniquely identifies each product in the Products table.

  2. FROM Products:

    • This specifies the table from which we are selecting the data. In this case, the table is named Products.

  3. WHERE low_fats = 'Y' AND recyclable = 'Y':

    • This is the condition that filters the rows in the Products table.

    • low_fats = 'Y' checks if the product is low fat.

    • recyclable = 'Y' checks if the product is recyclable.

    • The AND operator ensures that only the products that meet both conditions (low fat and recyclable) are selected.

Last updated