AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started
Mediumsqlnullaggregate

~90s

COUNT and NULL

Table employee:

emp_idnamemanager_id
1AshaNULL
2Ravi1
3Meena1
4SunilNULL
5Kiran2

What does this query return?

SELECT COUNT(*), COUNT(manager_id) FROM employee;
Show the answer and the worked solution

Correct answer: C.

5 and 3

Worked solution

  1. COUNT(*) counts rows whatever they contain, and the table has 5 rows.
  2. COUNT(manager_id) counts only the rows where that column is not NULL.
  3. Two rows, for Asha and Sunil, hold NULL there, leaving 3.
  4. The query therefore returns 5 and 3.

Answer: 5 and 3.

The idea

Every aggregate except COUNT(*) ignores NULLs, which also makes AVG(col) divide by the non-null count rather than the row count — a favourite source of surprising averages.

Shortcut. COUNT(column) = rows − NULLs in that column = 5 − 2 = 3, beside COUNT(*) = 5.

Common trap. "5 and 5" assumes COUNT(manager_id) counts every row, NULL included.

COUNT and NULL — OS, DBMS & Networks Aptitude Question

COUNT and NULL: Table employee: | empid | name | managerid | |--------|-------|------------| | 1 | Asha | NULL | |… Answer and worked solution on CodeKairo.

Question

Table employee:

emp_idnamemanager_id
1AshaNULL
2Ravi1
3Meena1
4SunilNULL
5Kiran2

What does this query return?

SELECT COUNT(*), COUNT(manager_id) FROM employee;

Options

  1. 3 and 3
  2. 3 and 5
  3. 5 and 3
  4. 5 and 5
Show the answer and the worked solution

Correct answer: C. 5 and 3

Worked solution

  1. COUNT(*) counts rows whatever they contain, and the table has 5 rows.
  2. COUNT(manager_id) counts only the rows where that column is not NULL.
  3. Two rows, for Asha and Sunil, hold NULL there, leaving 3.
  4. The query therefore returns 5 and 3.

Answer: 5 and 3.

The idea

Every aggregate except COUNT(*) ignores NULLs, which also makes AVG(col) divide by the non-null count rather than the row count — a favourite source of surprising averages.

Shortcut. COUNT(column) = rows − NULLs in that column = 5 − 2 = 3, beside COUNT(*) = 5.

Common trap. "5 and 5" assumes COUNT(manager_id) counts every row, NULL included.

All OS, DBMS & Networks questions · placement mock tests