SELECT name, dept_name FROM Employees JOIN Departments WHERE Employees.dept_id = Departments.dept_id;
medium
A. WHERE cannot be used with JOIN.
B. SELECT cannot have multiple columns.
C. Table names are incorrect.
D. Missing ON keyword for JOIN condition.
Solution
Step 1: Check JOIN syntax
JOIN requires ON keyword to specify join condition, not WHERE.
Step 2: Understand WHERE usage
WHERE filters rows after join; join condition must be in ON clause.
Final Answer:
Missing ON keyword for JOIN condition. -> Option D
Quick Check:
JOIN needs ON for condition = D [OK]
Hint: JOIN condition must use ON, not WHERE [OK]
Common Mistakes:
Using WHERE instead of ON for join condition
Thinking SELECT can't have multiple columns
Assuming table names are wrong
5. You have three tables: Orders(order_id, customer_id, product_id) Customers(customer_id, customer_name) Products(product_id, product_name) How would you write a query to list each order with the customer name and product name?
hard
A. SELECT order_id, customer_name, product_name FROM Orders JOIN Customers ON Orders.customer_id = Customers.customer_id JOIN Products ON Orders.product_id = Products.product_id;
B. SELECT order_id, customer_name, product_name FROM Orders, Customers, Products WHERE Orders.customer_id = Customers.customer_id;
C. SELECT order_id, customer_name, product_name FROM Orders LEFT JOIN Customers ON Orders.customer_id = Customers.customer_id;
D. SELECT order_id, customer_name, product_name FROM Customers JOIN Products ON Customers.customer_id = Products.product_id;
Solution
Step 1: Identify needed joins
Orders must join Customers on customer_id and Products on product_id to get names.
Step 2: Write correct JOIN syntax
Use JOIN with ON for both tables to link properly.
Step 3: Check other options
B misses the product_id join condition; C misses Products join; D joins unrelated keys.
Final Answer:
SELECT order_id, customer_name, product_name FROM Orders JOIN Customers ON Orders.customer_id = Customers.customer_id JOIN Products ON Orders.product_id = Products.product_id; -> Option A
Quick Check:
Correct JOINs on keys = A [OK]
Hint: Join all related tables on keys using ON [OK]