MYSQL Tutorial



MySQL SUM()


MySQL SUM() Function

The SUM() function in MySQL is used to calculate the total sum of a numeric column. It adds all the values in the specified column and returns the result.

Syntax:

SUM(column_name)
  

The column specified must be numeric, like INT, FLOAT, DECIMAL, etc. NULL values are ignored.

Example Queries:

-- Calculate total salaries of all employees
SELECT SUM(salary) AS total_salaries FROM employees;

-- Calculate total sales amount
SELECT SUM(sales_amount) AS total_sales FROM orders;

-- Calculate total salary for the 'Marketing' department
SELECT SUM(salary) AS marketing_total FROM employees WHERE department = 'Marketing';
  

Key Points:

  • SUM() only works with numeric columns.
  • NULL values in the column are ignored during summation.
  • Can be combined with WHERE clause to sum conditional data.
  • Often used with GROUP BY to calculate sums per category.

Example with GROUP BY:

-- Total salary grouped by department
SELECT department, SUM(salary) AS total_salary
FROM employees
GROUP BY department;
  

This query calculates the total salary for each department in the company.


🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review