---
title: "What is the difference between the COUNT() function and the SUM() function in SQL?"  
description: "What is the difference between the COUNT() function and the SUM() function in SQL?"  
author: "Revati S Misra"  
published: 2023-06-30  
updated: 2023-11-18  
canonical: https://www.mindstick.com/forum/158906/what-is-the-difference-between-the-count-function-and-the-sum-function-in-sql  
category: "mssql server"  
tags: ["mssql server", "sql server", "sql"]  
reading_time: 2 minutes  

---

# What is the difference between the COUNT() function and the SUM() function in SQL?

What is the [difference](https://www.mindstick.com/articles/157114/good-news-or-bad-news-and-the-difference-is) between the [COUNT](https://www.mindstick.com/forum/157774/selecting-count-with-distinct)() function and the [SUM](https://www.mindstick.com/forum/159332/how-can-i-use-sum-in-linq)() [function in SQL](https://www.mindstick.com/forum/156839/what-is-function-in-sql-server)?

## Replies

### Reply by Aryan Kumar

In [SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database), the **COUNT** and **SUM** functions serve different purposes and are used for different types of calculations.

### COUNT Function:

The **COUNT** [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) 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:

```plaintext
SELECT COUNT(column_name) FROM table_name WHERE condition;
```

- If **column_name** is specified, it counts the number of non-NULL values in that column.
- If ***** is used instead of **column_name**, it counts the total number of rows in the result set.

## Example:

```plaintext
-- Count the number of employees in the 'HR' department
SELECT COUNT(employee_id) FROM employees WHERE department = 'HR';
```

### 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:

```plaintext
SELECT SUM(column_name) FROM table_name WHERE condition;
```

- It is applicable only to numeric columns.

## Example:

```plaintext
-- Calculate the total salary of all employees
SELECT SUM(salary) FROM employees;
```

### Key Differences:

## Purpose:

- **COUNT** is used to count the number of rows or occurrences.
- **SUM** is used to calculate the sum of numeric values.

## Data Type:

- **COUNT** can be used with any data type, including text and NULL values.
- **SUM** is specifically used with numeric data types.

## Usage:

- **COUNT** can be used without specifying a column (**COUNT(*)**) to count all rows, or with a specific column to count non-NULL values in that column.
- **SUM** is used with a specific numeric column to calculate the sum of its values.

## NULL Handling:

- **COUNT** counts the number of non-NULL values.
- **SUM** includes NULL values in the calculation, treating them as 0.

## Example:

```plaintext
-- Count the number of employees and calculate the total salary
SELECT COUNT(employee_id) AS total_employees, SUM(salary) AS total_salary FROM employees;
```

In this example, we are using both **COUNT** and **SUM** to get the count of employees and the total salary in a single query.


---

Original Source: https://www.mindstick.com/forum/158906/what-is-the-difference-between-the-count-function-and-the-sum-function-in-sql

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
