SQL Functions & CASE · Question 12 · Medium

SQL Functions & CASE question 12

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

The two columns differ only in ELSE 0. What does the query return?

SELECT COUNT(CASE WHEN status = 'FAILED' THEN 1 ELSE 0 END),
       COUNT(CASE WHEN status = 'FAILED' THEN 1 END)
FROM Payment;
  1. 2, 2
  2. 6, 6
  3. 6, 2
  4. 2, 6
Answer and explanation

Answer: C. 6, 2

Explanation: COUNT counts every non-NULL value, and 0 is not NULL, so the first CASE (which yields 1 or 0) is counted for all 6 rows. The second CASE has no ELSE, so it yields NULL for successful payments and only the 2 failed ones are counted.

Previous: Table Payment: id | mode | amount | status 1 | UPI | 500 | SUCCESS 2 | CARD |… · Next: Table Visitor: id | city 1 | pune 2 | Delhi 3 | PUNE 4 | delhi 5 | Pune City…

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