DML & Transactions · Question 20 · Hard

DML & Transactions question 20

Table Customer:
id | name | total
1 | Omkar | 0
2 | Pallavi | 0
3 | Qasim | 0

Table Purchase:
cust_id | amount
1 | 400
3 | 250
1 | 300

What does the SELECT return after the UPDATE?

UPDATE Customer
SET total = (SELECT SUM(amount) FROM Purchase
             WHERE Purchase.cust_id = Customer.id);
SELECT name, total FROM Customer ORDER BY id;
  1. Omkar, 700; Pallavi, 0; Qasim, 250
  2. Omkar, 700; Qasim, 250
  3. Omkar, 400; Pallavi, 0; Qasim, 250
  4. Omkar, 700; Pallavi, NULL; Qasim, 250
Answer and explanation

Answer: D. Omkar, 700; Pallavi, NULL; Qasim, 250

Explanation: There is no WHERE on the UPDATE, so all three customers are updated with the result of the subquery. Omkar gets 400 + 300 and Qasim gets 250. Pallavi has no purchases, SUM over no rows is NULL, and that NULL replaces her 0.

Previous: Table Coupon(code PRIMARY KEY) holds one row, DIWALI. In PostgreSQL, MySQL…

Practise dml & transactions

Try a fresh set of dml & transactions questions with an explanation after every answer, free and without signing up: DML & Transactions practice. Or sit a full placement mock test.

More dml & transactions questions

More sql practice

SELECT, WHERE & ORDER BY · SQL Joins · Aggregates & GROUP BY · Subqueries & CTEs · Window Functions · NULL Handling · SQL Functions & CASE · UNION, INTERSECT & EXCEPT

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge