intermediateTop 30 Scenario-Based Questions
Write a query to find products that have never been ordered.
Method 1 (NOT EXISTS — preferred): SELECT p.* FROM products p WHERE NOT EXISTS (SELECT 1 FROM order_items oi WHERE oi.product_id = p.product_id); Method 2 (LEFT JOIN anti-join): SELECT p.* FROM products p LEFT JOIN order_items oi ON p.product_id = oi.product_id WHERE oi.order_id IS NULL; Method 3 (NOT IN — avoid NULLs): SELECT p.* FROM products p WHERE p.product_id NOT IN (SELECT product_id FROM o
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response