CS FundamentalsModerateTCS NQT · Infosys interview pattern

Table Orders(id, cust, city, amount) rows: (1, 'Ana', 'Pune', 500), (2, 'Ben', 'Delhi', NULL), (3, 'Ana', 'Pune', 300), (4, 'Chen', 'Delhi', 700), (5, 'Dia', NULL, 200), (6, 'Ben', 'Delhi', 700). How many rows does this query return, and what is the total shown for Delhi?

Table Orders(id, cust, city, amount) rows: (1, 'Ana', 'Pune', 500), (2, 'Ben', 'Delhi', NULL), (3, 'Ana', 'Pune', 300), (4, 'Chen', 'Delhi', 700), (5, 'Dia', NULL, 200), (6, 'Ben', 'Delhi', 700). How many rows does this query return, and what is the total shown for Delhi?
SELECT city, SUM(amount) FROM Orders GROUP BY city;
  1. A. 2 rows; Delhi = 1400
  2. B. 3 rows; Delhi = 1400
  3. C. 3 rows; Delhi = NULL
  4. D. 2 rows; Delhi = 2100

Correct answer: B. 3 rows; Delhi = 1400

GROUP BY puts all NULL cities into one group, so the groups are Pune, Delhi and NULL: 3 rows. SUM ignores the NULL amount in Delhi: 700 + 700 = 1400. A NULL inside a group does not make SUM NULL.

More DBMS and SQL questions

Exams that ask this topic