What would be the output of MySQL SUM() function if a column having no values has been passed as its argument?


When MySQL SUM() function got a column, having no values, as an argument then it will return NULL, rather than 0, as output. The column can be of any data type. Following the example, using a table named ‘social’ having only one column named ‘id’ with no values, will illustrate it

Example

mysql> Describe Social;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| Id    | int(11)     | YES  |     |   NULL  |       |
| Name  | varchar(20) | YES  |     |    NULL |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> Select * from Social;
Empty set (0.00 sec)

mysql> Select SUM(id) from Social;
+---------+
| SUM(id) |
+---------+
| NULL    |
+---------+
1 row in set (0.00 sec)

mysql> Select SUM(Name) from Social;
+-----------+
| SUM(Name) |
+-----------+
| NULL      |
+-----------+
1 row in set (0.00 sec)

Lakshmi Srinivas
Lakshmi Srinivas

Programmer / Analyst / Technician

Updated on: 07-Feb-2020

57 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements