What MySQL COUNT() function returns if there are some NULL values stored in a column also?

MySQLMySQLi Database

When we use MySQL COUNT() function to count the values stored in a column which also stored some NULL values then MySQL ignores the NULL and returns the result for only non-NULL values. To understand it, we are using the data, as follows, from table ‘Employee’ −

mysql> Select * from Employee;
+----+--------+--------+
| ID | Name   | Salary |
+----+--------+--------+
| 1  | Gaurav | 50000  |
| 2  | Rahul  | 20000  |
| 3  | Advik  | 25000  |
| 4  | Aarav  | 65000  |
| 5  | Ram    | 20000  |
| 6  | Mohan  | 30000  |
| 7  | Aryan  | NULL   |
| 8  | Vinay  | NULL   |
+----+--------+--------+
8 rows in set (0.00 sec)

Now, the following query applies COUNT() function on column ‘Salary’ −

mysql> Select COUNT(salary) from employee568;
+---------------+
| COUNT(salary) |
+---------------+
| 6             |
+---------------+
1 row in set (0.15 sec)

From the above result set, it is clear that MySQL ignores NULL and returns the count for only non-NULL values.

raja
Published on 21-Feb-2018 07:05:58
Advertisements