1757. Recyclable and Low Fat Products
Problem Statement
Table: Products
Column Name | Type |
---|---|
product_id | int |
low_fats | enum |
recyclable | enum |
product_id
is theprimary key
(column with unique values) for this table.
low_fats
is anENUM
(category) of type ('Y', 'N') where 'Y' means this product is low fat and 'N' means it is not.
recyclable
is anENUM
(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
andrecyclable
. - Return the result in
any order
. - The query result format is in the following example:
Example
Input: Products
table
product_id | low_fats | recyclable |
---|---|---|
0 | Y | N |
1 | Y | Y |
2 | N | Y |
3 | Y | Y |
4 | N | N |
Output:
product_id |
---|
1 |
3 |
Explanation:
The products with
id
1 and 3 are bothlow fat
andrecyclable
.
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 theSELECT
statement, which specifies the data you want to retrieve from the database. In this case, you're selecting theproduct_id
column, so the query will return the product IDs.FROM Products
: This is theFROM
clause, which specifies the table you want to retrieve data from. In this case, you're retrieving data from theProducts
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 thatlow_fats
must be equal to 'Y' (low_fats = 'Y'
) andrecyclable
must be equal to 'Y' (recyclable = 'Y'
).low_fats = 'Y'
: This condition is true for rows wherelow_fats
is equal to 'Y'.recyclable = 'Y'
: This condition is true for rows whererecyclable
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