Home > Database > Mysql Tutorial > How does the MySQL SUM() function evaluate whether a column also has a NULL value?

How does the MySQL SUM() function evaluate whether a column also has a NULL value?

PHPz
Release: 2023-09-14 08:09:06
forward
937 people have browsed it

MySQL SUM() 函数如何评估列是否也有 NULL 值?

Suppose we are calculating the sum of the values ​​of a column that also has NULL values, then the MySQL SUM() function ignores the NULL values ​​and calculates the sum of the remaining values. To understand it, consider the following example of table "employee" which contains the following details -

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)
Copy after login

Now, suppose we want to get the total salary of the employees in the above table, then while calculating it using SUM() function, it ignores NULL values. The following query will produce the desired result set -

mysql> Select SUM(Salary) from Employee;
+-------------+
| SUM(Salary) |
+-------------+
| 210000      |
+-------------+
1 row in set (0.00 sec)
Copy after login

You can also verify it with the help of the following query -

mysql> Select SUM(Salary) from Employee WHERE Salary IS NOT NULL;
+-------------+
| SUM(Salary) |
+-------------+
|   210000    |
+-------------+
1 row in set (0.00 sec)
Copy after login

The above is the detailed content of How does the MySQL SUM() function evaluate whether a column also has a NULL value?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template