Skip to content

1757. Recyclable and Low Fat Products

Problem Statement

Table: Products

Column NameType
product_idint
low_fatsenum
recyclableenum

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 a solution to find the ids of the products that are both low fat and recyclable.
  • Return the result in any order.
  • The query result format is in the following example:

Example

Input: Products table

product_idlow_fatsrecyclable
0YN
1YY
2NY
3YY
4NN

Output:

product_id
1
3

Explanation:

The products with id 1 and 3 are both low fat and recyclable.

Submissions

sql
SELECT product_id
FROM Products
WHERE low_fats = 'Y' AND recyclable = 'Y';
python
import pandas as pd

def find_products(products: pd.DataFrame) -pd.DataFrame:
    return products.query("low_fats == 'Y' and recyclable == 'Y'")[['product_id']]

Explanations

PostgreSQL
Submitted by @noeyislearning
  • SELECT product_id: This is the SELECT statement, which specifies the data you want to retrieve from the database. In this case, you're selecting the product_id column, so the query will return the product IDs.
  • FROM Products: This is the FROM clause, which specifies the table you want to retrieve data from. In this case, you're retrieving data from the Products table.
  • WHERE low_fats = 'Y' AND recyclable = 'Y': This is the WHERE clause, which specifies the conditions that the rows must meet to be included in the result set. In this case, the conditions are that low_fats must be equal to 'Y' (low_fats = 'Y') and recyclable must be equal to 'Y' (recyclable = 'Y').
    • low_fats = 'Y': This condition is true for rows where low_fats is equal to 'Y'.
    • recyclable = 'Y': This condition is true for rows where recyclable is equal to 'Y'.
    • AND: This is a logical operator that combines the two conditions. A row will be included in the result set only if both conditions are true.
Pandas
Submitted by @noeyislearning