Bird
0
0

Given tables products(id, name), sales(product_id, quantity), write a query to find products with no sales and show their name and zero as quantity.

hard📝 Application Q9 of 15
SQL - LEFT and RIGHT JOIN
Given tables products(id, name), sales(product_id, quantity), write a query to find products with no sales and show their name and zero as quantity.
ASELECT p.name, COALESCE(s.quantity, 0) AS quantity FROM products p LEFT JOIN sales s ON p.id = s.product_id WHERE s.product_id IS NULL;
BSELECT p.name, s.quantity FROM products p INNER JOIN sales s ON p.id = s.product_id WHERE s.quantity = 0;
CSELECT p.name, 0 AS quantity FROM products p RIGHT JOIN sales s ON p.id = s.product_id WHERE p.id IS NULL;
DSELECT p.name, s.quantity FROM products p FULL JOIN sales s ON p.id = s.product_id WHERE s.product_id IS NULL;
Step-by-Step Solution
Solution:
  1. Step 1: LEFT JOIN products with sales

    All products included; sales matched or NULL if none.
  2. Step 2: Filter products with no sales

    WHERE s.product_id IS NULL selects products without sales.
  3. Step 3: Use COALESCE to show zero quantity

    COALESCE(s.quantity, 0) replaces NULL with 0 for display.
  4. Final Answer:

    SELECT p.name, COALESCE(s.quantity, 0) AS quantity FROM products p LEFT JOIN sales s ON p.id = s.product_id WHERE s.product_id IS NULL; -> Option A
  5. Quick Check:

    LEFT JOIN + NULL filter + COALESCE = unmatched with zero [OK]
Quick Trick: Use COALESCE with LEFT JOIN NULL filter to show zero [OK]
Common Mistakes:
MISTAKES
  • Using INNER JOIN excludes unmatched products
  • Not replacing NULL with zero
  • Wrong join type or filter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes