Databricks Data Engineer Associate Question 184
Select 2You are working with two datasets in Databricks: orders and customers. The orders dataset has columns order_id, customer_id, and amount, while the customers dataset has columns customer_id and customer_name. You execute the following query to join these datasets:
SELECT o.order_id, o.amount, c.customer_name FROM orders o LEFT JOIN customers c ON o.customer_id = c.customer_id;
Which result will be returned by this query?
- A
All rows from the
orderstable with matching rows from thecustomerstable. If no match is found,customer_namewill be NULL. - B
Only rows from the
orderstable where there is a matchingcustomer_idin thecustomerstable. - C
All rows from the
customerstable with matching rows from theorderstable. If no match is found,order_idandamountwill be NULL. - D
The result will include rows where
customer_idexists in bothordersandcustomers, and rows fromorderswith NULL values forcustomer_nameif no match is found incustomers. - E
Rows where
customer_idexists only in thecustomerstable will be included in the result, even if they have no matching rows in theorderstable.
Show answer and explanation
Correct answers: A, D
Explanation
A LEFT JOIN in SQL returns all rows from the left table (in this case, orders), and the matching rows from the right table (customers). If there is no match, the result will include NULL values for columns from the right table. This ensures that no rows are excluded from the orders table, even if there is no corresponding record in the customers table.
- A. Correct.
Correct: This is the expected behavior of a LEFT JOIN. All rows from the
orderstable will be included, and if there is no matchingcustomer_idin thecustomerstable, thecustomer_namecolumn will have NULL values. - B. Incorrect.
Incorrect: This describes the behavior of an INNER JOIN, not a LEFT JOIN.
- C. Incorrect.
Incorrect: This describes the behavior of a RIGHT JOIN, not a LEFT JOIN. A LEFT JOIN prioritizes the left table (
ordersin this case). - D. Correct.
Correct: This is another way to describe the expected behavior of a LEFT JOIN, where unmatched rows from the
orderstable will have NULL values for columns from thecustomerstable. - E. Incorrect.
Incorrect: Rows from the
customerstable without matching rows in theorderstable will not appear in the result of a LEFT JOIN. This describes the behavior of a FULL OUTER JOIN.