Recyclable and Low Fat Products - Problem

๐ŸŒฑ Recyclable and Low Fat Products

You're working as a data analyst for a health-conscious grocery store chain that wants to promote environmentally friendly and healthy products. The store has a comprehensive database of all products with their nutritional and environmental attributes.

Given a Products table with the following structure:

Column NameType
product_idint
low_fatsenum ('Y', 'N')
recyclableenum ('Y', 'N')

Where:

  • product_id is the unique identifier for each product
  • low_fats indicates if the product is low in fat ('Y' = Yes, 'N' = No)
  • recyclable indicates if the product packaging is recyclable ('Y' = Yes, 'N' = No)

Your mission: Find all products that are both low fat and recyclable to feature in the store's new "Healthy & Green" promotion campaign.

Return the product IDs in any order.

Input & Output

Basic Example - Mixed Product Types
$ 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 | +-------------+
๐Ÿ’ก Note: Products 1 and 3 are the only ones that satisfy both conditions: low_fats = 'Y' AND recyclable = 'Y'. Product 0 is low fat but not recyclable, Product 2 is recyclable but not low fat, and Product 4 satisfies neither condition.
All Products Qualify
$ Input: Products table: +-------------+----------+------------+ | product_id | low_fats | recyclable | +-------------+----------+------------+ | 10 | Y | Y | | 20 | Y | Y | | 30 | Y | Y | +-------------+----------+------------+
โ€บ Output: +-------------+ | product_id | +-------------+ | 10 | | 20 | | 30 | +-------------+
๐Ÿ’ก Note: All products in this healthy store satisfy both conditions, so all product IDs are returned.
No Products Qualify
$ Input: Products table: +-------------+----------+------------+ | product_id | low_fats | recyclable | +-------------+----------+------------+ | 100 | N | N | | 101 | Y | N | | 102 | N | Y | +-------------+----------+------------+
โ€บ Output: +-------------+ | product_id | +-------------+ +-------------+
๐Ÿ’ก Note: No products satisfy both conditions simultaneously. Product 101 is low fat but not recyclable, Product 102 is recyclable but not low fat, and Product 100 satisfies neither condition. The result is an empty set.

Constraints

  • 1 โ‰ค Number of rows in Products table โ‰ค 1000
  • product_id is unique for each row
  • low_fats and recyclable are ENUM types with values ('Y', 'N')
  • All ENUM values are guaranteed to be either 'Y' or 'N'

Visualization

Tap to expand
Smart Product Filtering SystemProductsDatabaseHealthScannerEcoScannerHealthy &GreenProductsID: 1Low Fat: YRecyclable: YID: 2Low Fat: NRecyclable: YID: 3Low Fat: YRecyclable: YID: 1ID: 3SQL: WHERE low_fats = 'Y' AND recyclable = 'Y'
Understanding the Visualization
1
Products Enter Scanner
Each product passes through a dual-sensor scanner that checks both nutritional and environmental properties
2
Health Check
First sensor verifies if the product is low in fat (low_fats = 'Y')
3
Environmental Check
Second sensor verifies if the packaging is recyclable (recyclable = 'Y')
4
Decision Gate
Only products passing BOTH checks are directed to the 'Healthy & Green' collection bin
Key Takeaway
๐ŸŽฏ Key Insight: The AND operator in SQL acts like a quality gate that requires ALL conditions to be true simultaneously, making it perfect for multi-criteria filtering scenarios like this health and environmental compliance check.
Asked in
Amazon 25 Microsoft 18 Google 15 Meta 12
78.5K Views
High Frequency
~5 min Avg. Time
3.3K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen