What is the difference between the COUNT() function and the SUM() function in SQL?
What is the difference between the COUNT() function and the SUM() function in SQL?
405
30-Jun-2023
Updated on 18-Nov-2023
Aryan Kumar
18-Nov-2023In SQL, the COUNT and SUM functions serve different purposes and are used for different types of calculations.
COUNT Function:
The COUNT function is used to count the number of rows in a result set or the number of occurrences of a particular value in a column. It is often used in combination with the GROUP BY clause to get counts for groups of data.
Syntax:
Example:
SUM Function:
The SUM function, on the other hand, is used to calculate the sum of numerical values in a column. It adds up the numeric values in the specified column.
Syntax:
Example:
Key Differences:
Purpose:
Data Type:
Usage:
NULL Handling:
Example:
In this example, we are using both COUNT and SUM to get the count of employees and the total salary in a single query.