1 minute read

Table: Products

+————-+———+ | Column Name | Type | +————-+———+ | product_id | int | | low_fats | enum | | recyclable | enum | +————-+———+ In SQL, product_id is the primary key for this table. low_fats is an ENUM of type (‘Y’, ‘N’) where ‘Y’ means this product is low fat and ‘N’ means it is not. recyclable is an ENUM of types (‘Y’, ‘N’) where ‘Y’ means this product is recyclable and ‘N’ means it is not.

Find the ids of products that are both low fat and recyclable.

Return the result table in any order.

The result format is in the following example.

Example 1:

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: Only products 1 and 3 are both low fat and recyclable.

/* Write your MySQL query statement below */
SELECT product_id FROM products WHERE low_fats = 'Y' AND recyclable = 'Y'

SELECT product_id: This part of the query specifies the column(s) you want to retrieve from the products table. In this case, you want to retrieve the product_id column.

FROM products: This part of the query specifies the table you want to query. In this case, you want to retrieve data from the products table.

WHERE low_fats = 'Y' AND recyclable = 'Y': This is the filter condition for the query. It restricts the result to rows where both the low_fats column and the recyclable column have the value ‘Y’. In other words, it selects only those products that are both low in fats and recyclable.

Leave a comment