For the following question it is said that the answer should be C. But I think the correct answer is Answer D as NOT MATCHED block inserts all unmatching records to target table. Can anybody explain this? Thank you.
Q)View the Exhibit and examine the data in ORDERS_MASTER and MONTHLY_ORDERS tables.
Evaluate the following MERGE statement:
MERGE INTO orders_master o
USING monthly_orders m
ON (o.order_id = m.order_id)
WHEN MATCHED THEN
UPDATE SET o.order_total = m.order_total
DELETE WHERE (m.order_total IS NULL)
WHEN NOT MATCHED THEN
INSERT VALUES (m.order_id, m.order_total);
What would be the outcome of the above statement?
A. The ORDERS_MASTER table would contain the ORDER_IDs 1 and 2.
B. The ORDERS_MASTER table would contain the ORDER_IDs 1,2 and 3.
C. The ORDERS_MASTER table would contain the ORDER_IDs 1,2 and 4.
D. The ORDERS_MASTER table would contain the ORDER IDs 1,2,3 and 4.
Answer: C
The correct answer is indeed C, this is because the source of the merge operation is the
monthly_orders
table, which only contains two records withorder_id
2 and 3 respectively.Think about what will happen for each of these records:
order_id = 2
, because this id exists in theorder_master
table, we'll execute theMATCHED
part of the merge statement, updating theorder_total
to 2500. Since the quantity for this record is notNULL
, theDELETE
won't do anything.order_id = 3
, again, the id exists in theorder_master
table, so we execute theMATCHED
part of the merge statement, updating theorder_total
toNULL
and then issuing a DELETE onorder_master
for the row we just updated because the quantity onmonthly_order
isNULL
.This leaves us with
order_id
1, 2 and 4, which matches answer C.Code