Skip to content
>_sqlbuddy

Recyclable and Low Fat Products

Find the ids of products that are both low fat and recyclable.

Start practice

Problem 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_fats and recyclable are flags: 'Y' means yes, 'N' means no, and `NULL` means unknown — an unknown value is not a yes.

Example

product_idlow_fatsrecyclable
1YN
2YY
3NY

Expected result:

product_id
2

Constraints

  • products.product_id is the primary key.
  • low_fats and recyclable are nullable.

Tips

  • A plain WHERE low_fats = 'Y' AND recyclable = 'Y' already handles NULL correctly — a NULL never 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

Related questions

Learn more