NULL Handling · Question 19 · Hard

NULL Handling question 19

Table Parcel:
id | city | agent
1 | Pune | Ravi
2 | Pune | NULL
3 | Indore | NULL
4 | Indore | Ravi
5 | Surat | Meena

What does this query return?

SELECT agent, COUNT(*), COUNT(agent)
FROM Parcel
GROUP BY agent
HAVING COUNT(agent) = 0;
  1. No rows
  2. NULL, 2, 2
  3. NULL, 0, 0
  4. NULL, 2, 0
Answer and explanation

Answer: D. NULL, 2, 0

Explanation: GROUP BY puts all NULL agents into one group. In that group COUNT(*) counts its 2 rows, while COUNT(agent) counts non-NULL values and gives 0, so it is the only group that passes the HAVING test.

Previous: Table Parcel: id | city | agent 1 | Pune | Ravi 2 | Pune | NULL 3 | Indore |… · Next: In SQL's three-valued logic, which of these expressions evaluates to FALSE…

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

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