SQL Functions & CASE · Question 11 · Medium
SQL Functions & CASE question 11
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 return?
SELECT mode,
SUM(CASE WHEN status = 'SUCCESS' THEN amount ELSE 0 END)
FROM Payment
GROUP BY mode
ORDER BY mode;
- CARD, 1200; CASH, 250; UPI, 1200
- CARD, 1600; CASH, 250; UPI, 1500
- CARD, 1200; CASH, 250; UPI, 500
- CARD, 1; CASH, 1; UPI, 2
Answer and explanation
Answer: A. CARD, 1200; CASH, 250; UPI, 1200
Explanation: Inside each group, the CASE passes the amount to SUM only for successful payments and passes 0 otherwise. CARD: 1200 + 0. CASH: 250. UPI: 500 + 0 + 700 = 1200.
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
- 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…
- 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 Visitor: id | city 1 | pune 2 | Delhi 3 | PUNE 4 | delhi 5 | Pune City…
- Table Salary: emp | basic | bonus Nikhil | 30000 | 5000 Ojas | 40000 | NULL…
- 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 Payment: id | mode | amount | status 1 | UPI | 500 | SUCCESS 2 | CARD |…
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