Leetcode has a Study Plan that focuses on solving SQL problems. The basic plan contains 50 problems, and when you finish them, you are awarded a cool badge. In between my many university assignments, I decided to take on this challenge as a goal, and of course, I’ll share my thoughts with you.

The first problem in the challenge is:

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
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.

Solution

This is a good practice question that you should be able to solve after the first SQL lesson. We’ll use a query to access the Products table and select only the product_id column for products with low_fats='Y' (indicating low-fat content) and recyclable='Y'.

1
2
3
4
--- Write your MySQL query statement below
SELECT product_id  
FROM Products
WHERE low_fats = 'Y' AND recyclable = 'Y';

The output is:

1
2
3
4
| product_id |
| ---------- |
| 1          |
| 3          |

That’s all.
Piece of cake, right?


Cover image by Do Exploit from PixelBay