Which of the following SQL queries correctly uses a JOIN to get all customers and their orders?
easy📝 Syntax Q12 of 15
SQL - Subqueries
Which of the following SQL queries correctly uses a JOIN to get all customers and their orders?
ASELECT customers.name, orders.id FROM customers JOIN orders ON customers.id = orders.customer_id;
BSELECT customers.name, orders.id FROM customers WHERE customers.id = orders.customer_id;
CSELECT customers.name, orders.id FROM customers, orders WHERE customers.id == orders.customer_id;
DSELECT customers.name, orders.id FROM customers JOIN orders ON customers.customer_id = orders.id;
Step-by-Step Solution
Solution:
Step 1: Check JOIN syntax
Correct JOIN syntax uses ON with matching keys: customers.id = orders.customer_id.
Step 2: Validate each option
SELECT customers.name, orders.id FROM customers JOIN orders ON customers.id = orders.customer_id; uses correct JOIN and ON condition. SELECT customers.name, orders.id FROM customers WHERE customers.id = orders.customer_id; uses WHERE without JOIN, which is invalid here. SELECT customers.name, orders.id FROM customers, orders WHERE customers.id == orders.customer_id; uses double equals (==) which is invalid in SQL. SELECT customers.name, orders.id FROM customers JOIN orders ON customers.customer_id = orders.id; reverses keys incorrectly.
Final Answer:
SELECT customers.name, orders.id FROM customers JOIN orders ON customers.id = orders.customer_id; -> Option A
Quick Check:
Correct JOIN syntax = SELECT customers.name, orders.id FROM customers JOIN orders ON customers.id = orders.customer_id; [OK]
Quick Trick:JOIN uses ON with matching keys, not WHERE or == [OK]
Common Mistakes:
MISTAKES
Using WHERE instead of ON for JOIN condition
Using == instead of = in SQL
Mixing up key columns in ON clause
Master "Subqueries" in SQL
9 interactive learning modes - each teaches the same concept differently