DEV Community

Cover image for SQL 50 – 1757. Recyclable and Low Fat Products
Ben Pereira
Ben Pereira

Posted on

SQL 50 – 1757. Recyclable and Low Fat Products

It’s an easy problem, the first one from SQL 50 with the description being:

Table: Products

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| 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.

Write a solution to 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.

As it proposes you need to return a list of ids of products, that are both low fat and recyclable, any order and there format example.

For this resolution first you will select the product_id (which the id required) from the table Products and where the condition is that low_fats and recyclable are true, meaning ‘Y’:

select product_id from Products where low_fats = 'Y' and recyclable = 'Y';
Enter fullscreen mode Exit fullscreen mode

Runtime: 1530 ms, faster than 17.91% of MySQL online submissions for Recyclable and Low Fat Products.

Memory Usage: 0B, less than 100.00% of MySQL online submissions for Recyclable and Low Fat Products.

Feels like a beginning of an interview, very light and simple to start up a context and more complex questions regarding same matter e.g. percentages, averages, specific scenarios, etc.


That’s it! If there is anything thing else to discuss feel free to drop a comment, if I missed anything let me know so I can update accordingly.

Until next post! :)

Top comments (0)