Recyclable and Low Fat Products
Find the ids of products that are both low fat and recyclable.
Start practiceProblem statement
Prompt
Given the products table below, write a query that returns the product_id of every product that is both low fat and recyclable.
- Output a single column:
product_id. - The order of the result does not matter.
low_fatsandrecyclableare flags:'Y'means yes,'N'means no, and `NULL` means unknown — an unknown value is not a yes.
Example
| product_id | low_fats | recyclable |
|---|---|---|
| 1 | Y | N |
| 2 | Y | Y |
| 3 | N | Y |
Expected result:
| product_id |
|---|
| 2 |
Constraints
products.product_idis the primary key.low_fatsandrecyclableare nullable.
Tips
- A plain
WHERE low_fats = 'Y' AND recyclable = 'Y'already handlesNULLcorrectly — aNULLnever equals'Y'.
Schema
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
low_fats TEXT, -- 'Y' | 'N' | NULL
recyclable TEXT -- 'Y' | 'N' | NULL
);
Sample data
INSERT INTO products (product_id, low_fats, recyclable) VALUES
(1, 'Y', 'N'),
(2, 'Y', 'Y'),
(3, 'N', 'Y'),
(4, 'Y', 'Y'),
(5, 'N', 'N');
Additional hidden fixtures are applied during validation to test edge cases.
Solution
One correct approach — try solving it yourself in the practice editor first, then compare. There is usually more than one valid solution.
-- Reference: products that are both low fat and recyclable.
SELECT product_id
FROM products
WHERE low_fats = 'Y' AND recyclable = 'Y'
ORDER BY product_id;
Key concepts
- SELECT
- WHERE
- Boolean filtering
- NULL handling