NULL Handling · Question 12 · Medium
NULL Handling question 12
Table Staff:
id | name | dept | bonus
1 | Anita | HR | 5000
2 | Bhavesh | IT | NULL
3 | Charu | IT | 8000
4 | Deepak | Sales | NULL
5 | Esha | HR | 3000
What does this query return for the three departments?
SELECT dept, SUM(bonus)
FROM Staff
GROUP BY dept
ORDER BY dept;
- HR, 8000; IT, 8000
- HR, 8000; IT, 8000; Sales, 0
- HR, 8000; IT, NULL; Sales, NULL
- HR, 8000; IT, 8000; Sales, NULL
Answer and explanation
Answer: D. HR, 8000; IT, 8000; Sales, NULL
Explanation: SUM skips NULLs, so IT is just 8000 and HR is 5000 + 3000. The Sales group still exists, but it has no non-NULL bonus to add, and SUM over no values is NULL, not 0.
Previous: Table Parcel: id | city | agent 1 | Pune | Ravi 2 | Pune | NULL 3 | Indore |… · Next: Table Feedback: id | student | rating 1 | Tanvi | 4 2 | Umesh | 0 3 | Varun |…
Practise null handling
Try a fresh set of null handling questions with an explanation after every answer, free and without signing up: NULL Handling practice. Or sit a full placement mock test.
More null handling questions
- Table Staff: id | name | dept | bonus 1 | Anita | HR | 5000 2 | Bhavesh | IT |…
- Table Staff: id | name | dept | bonus 1 | Anita | HR | 5000 2 | Bhavesh | IT |…
- Table Parcel: id | city | agent 1 | Pune | Ravi 2 | Pune | NULL 3 | Indore |…
- Table Feedback: id | student | rating 1 | Tanvi | 4 2 | Umesh | 0 3 | Varun |…
- Table Customer: id | name | mobile | email 1 | Farhan | 9810012345 | NULL 2 |…
- A table Voter has 10 rows, and the city column is NULL in exactly 3 of them…
- Table Student: roll | name 1 | Irfan 2 | Jaya 3 | Kunal 4 | Lata Table Hostel…
- Table Staff: id | name | dept | bonus 1 | Anita | HR | 5000 2 | Bhavesh | IT |…
- Table Parcel: id | city | agent 1 | Pune | Ravi 2 | Pune | NULL 3 | Indore |…
- Table Parcel: id | city | agent 1 | Pune | Ravi 2 | Pune | NULL 3 | Indore |…
More sql practice
SQL Functions & CASE · UNION, INTERSECT & EXCEPT · Tables, Keys & Constraints · DML & Transactions · SELECT, WHERE & ORDER BY · SQL Joins · Aggregates & GROUP BY · Subqueries & CTEs
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge