Bird
0
0

Given tables orders(order_id, customer_id) and customers(customer_id, name), which query lists all customers including those without any orders?

hard📝 Application Q8 of 15
PostgreSQL - Joins in PostgreSQL
Given tables orders(order_id, customer_id) and customers(customer_id, name), which query lists all customers including those without any orders?
ASELECT customers.name, orders.order_id FROM customers RIGHT JOIN orders ON customers.customer_id = orders.customer_id;
BSELECT customers.name, orders.order_id FROM orders LEFT JOIN customers ON orders.customer_id = customers.customer_id;
CSELECT customers.name, orders.order_id FROM customers INNER JOIN orders ON customers.customer_id = orders.customer_id;
DSELECT customers.name, orders.order_id FROM customers LEFT JOIN orders ON customers.customer_id = orders.customer_id;
Step-by-Step Solution
Solution:
  1. Step 1: Understand requirement

    List all customers, including those without orders.
  2. Step 2: Choose correct JOIN type

    LEFT JOIN with customers as left table includes all customers.
  3. Step 3: Evaluate options

    SELECT customers.name, orders.order_id FROM customers LEFT JOIN orders ON customers.customer_id = orders.customer_id; correctly uses LEFT JOIN from customers to orders. SELECT customers.name, orders.order_id FROM orders LEFT JOIN customers ON orders.customer_id = customers.customer_id; reverses tables, excluding customers without orders. SELECT customers.name, orders.order_id FROM customers INNER JOIN orders ON customers.customer_id = orders.customer_id; uses INNER JOIN, excluding customers without orders. SELECT customers.name, orders.order_id FROM customers RIGHT JOIN orders ON customers.customer_id = orders.customer_id; uses RIGHT JOIN but with reversed tables, not meeting requirement.
  4. Final Answer:

    SELECT customers.name, orders.order_id FROM customers LEFT JOIN orders ON customers.customer_id = orders.customer_id; -> Option D
  5. Quick Check:

    LEFT JOIN keeps all rows from left table [OK]
Quick Trick: Use LEFT JOIN with main table on left [OK]
Common Mistakes:
  • Using INNER JOIN excludes unmatched rows
  • Reversing table order in LEFT JOIN
  • Using RIGHT JOIN incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes