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

This is a Pro chapter

Sign in, then upgrade to Pro or Power to unlock this and the full Databases Mastery library.

Write a query to find products that have never been ordered.

Next Step

Continue to A PostgreSQL sequence is out of sync after a bulk INSERT using explicit IDs. How do you fix it?← Back to all SQL questions