Recyclable and Low Fat Products – 1757. LeetCode

The LeetCode problem “Recyclable and Low Fat Products” is a SQL problem where the object is to retrieve a list of products that are both recyclable and low fat from a database table. Each product has attributes indicating whether it is recyclable or low fat.

Problem Objective

Given a Products table with the following columns:

• product_id: unique identifier for each product.

• low_fats: string (‘Y’ or ‘N’) indicating if the product is low fat.

• recyclable: string (‘Y’ or ‘N’) indicating if the product is recyclable.

The goal is to write a SQL query that selects the product_id of products that are both low fat and recyclable.


Framework for Solving the Problem

To solve this problem:

1. Apply Filtering Conditions: Use SQL’s WHERE clause to filter rows where both low_fats and recyclable columns are marked as ‘Y’.

2. Select Relevant Data: Use SELECT to choose only the product_name column for the qualifying products.


This problem is a straightforward example of conditional filtering in SQL, where we need to filter rows based on multiple conditions. This approach is common in SQL when dealing with attributes that qualify records for selection. Additionally, it shows how SQL’s WHERE clause can be used to extract specific records by applying simple boolean logic.

SQL Query Solution

SELECT product_id
FROM Products
WHERE low_fats = 'Y' AND recyclable = 'Y';

1. SELECT product_name:

• Specifies that we want to retrieve only the product_name column, as the question only asks for the names of products that meet the criteria.

2. FROM Products:

• Identifies Products as the table from which data is being retrieved.

3. WHERE low_fats = ‘Y’ AND recyclable = ‘Y’:

• Filters rows to include only those where both low_fats and recyclable columns are ‘Y’.

• The AND operator ensures that both conditions must be true for a row to be included in the output.

How This SQL Solves the Problem

Filters the Data: Selects only products that are both recyclable and low fat, narrowing down the dataset based on conditions.

Retrieves Target Column: Returns only the product names, as required by the problem.

This solution reflects the broader context of SQL’s conditional filtering pattern, which allows efficient data retrieval by applying specific conditions to focus on relevant subsets of data.

Leave a Reply

Your email address will not be published. Required fields are marked *