When do we use COUNT() or SUM()?



When do we use the COUNT() function or the SUM() function?


Although they might appear to perform a similar task, the COUNT() and SUM() functions have very different uses.

COUNT() is used to take a name of a column, and counts the number of non-empty values in that column. COUNT() does not take into account the actual values stored, and only cares if they have a non-empty value. Each row is essentially counted as 1 towards the total count.

On the other hand, SUM() takes a column name, and returns the sum of all values in the column, meaning that it must take into account the actual values stored.

In general, use COUNT() when you want to count how many rows contain a non-empty value for a specified column. Use SUM() when you want to get the total sum of all values in a column.