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.
SUM(column_name)
The column specified must be numeric, like INT, FLOAT, DECIMAL, etc. NULL values are ignored.
-- 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';
WHERE
clause to sum conditional data.GROUP BY
to calculate sums per category.-- 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.
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!