SQL Functions & CASE · Question 17 · Hard

SQL Functions & CASE question 17

Table Payment:
id | mode | amount | status
1 | UPI | 500 | SUCCESS
2 | CARD | 1200 | SUCCESS
3 | UPI | 300 | FAILED
4 | UPI | 700 | SUCCESS
5 | CARD | 400 | FAILED
6 | CASH | 250 | SUCCESS

No payment in the table was made by WALLET. What does this query return?

SELECT SUM(CASE WHEN mode = 'WALLET' THEN amount END),
       COUNT(CASE WHEN mode = 'WALLET' THEN 1 END)
FROM Payment;
  1. 0, 0
  2. NULL, NULL
  3. NULL, 0
  4. 0, NULL
Answer and explanation

Answer: C. NULL, 0

Explanation: Neither CASE has an ELSE, so both give NULL for every row. SUM over only NULLs is NULL, while COUNT over only NULLs is 0. Writing ELSE 0 inside the SUM would make it return 0.

Previous: Table Student: roll | name | marks 1 | Aarav | 82 2 | Diya | 67 3 | Kabir | 45… · Next: Table Payment: id | mode | amount | status 1 | UPI | 500 | SUCCESS 2 | CARD |…

Practise sql functions & case

Try a fresh set of sql functions & case questions with an explanation after every answer, free and without signing up: SQL Functions & CASE practice. Or sit a full placement mock test.

More sql functions & case questions

More sql practice

UNION, INTERSECT & EXCEPT · Tables, Keys & Constraints · DML & Transactions · SELECT, WHERE & ORDER BY · SQL Joins · Aggregates & GROUP BY · Subqueries & CTEs · Window Functions

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