SQL Functions & CASE · Question 18 · Hard
SQL Functions & CASE question 18
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
What does this query with GROUP BY and HAVING return?
SELECT mode,
SUM(CASE WHEN status = 'SUCCESS' THEN 1 ELSE 0 END),
COUNT(*)
FROM Payment
GROUP BY mode
HAVING SUM(CASE WHEN status = 'FAILED' THEN 1 ELSE 0 END) > 0
ORDER BY mode;
- CARD, 1, 2; CASH, 1, 1; UPI, 2, 3
- CARD, 1, 1; UPI, 2, 1
- CARD, 2, 2; UPI, 3, 3
- CARD, 1, 2; UPI, 2, 3
Answer and explanation
Answer: D. CARD, 1, 2; UPI, 2, 3
Explanation: HAVING keeps only modes with at least one failed payment, which removes CASH. For each remaining mode the first sum counts successes and COUNT(*) counts all payments: CARD has 1 of 2 and UPI has 2 of 3.
Previous: Table Payment: id | mode | amount | status 1 | UPI | 500 | SUCCESS 2 | CARD |… · Next: Table Student: roll | name | marks 1 | Aarav | 82 2 | Diya | 67 3 | Kabir | 45…
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
- Which statement about the SQL CASE expression is correct?
- Table Student: roll | name | marks 1 | Aarav | 82 2 | Diya | 67 3 | Kabir | 45…
- Table Payment: id | mode | amount | status 1 | UPI | 500 | SUCCESS 2 | CARD |…
- Table Student: roll | name | marks 1 | Aarav | 82 2 | Diya | 67 3 | Kabir | 45…
- Table Student: roll | name | marks 1 | Aarav | 82 2 | Diya | 67 3 | Kabir | 45…
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