SQL Sum

The SQL SUM function is used to calculate the total sum of a numeric column by adding all of the numeric values. This function is useful when you need to get the total values in a dataset.

SQL Sum Syntax

SELECT SUM(column_name)
FROM table_name
WHERE condition;



SQL Sum Example

SQL Sum with All Rows Example

This query sums up the total length of all contact numbers from the Patients table.

SELECT SUM(LENGTH(contact_number))
FROM Patients;




SQL Sum with a WHERE Clause

This query calculates the total length of contact numbers for all female patients. The WHERE clause limits the rows to those where gender = ‘F’

SELECT SUM(LENGTH(contact_number))
FROM Patients
WHERE gender = 'F';




SQL Sum with Group By Clause

This query calculates the total length of contact numbers for each gender group. The GROUP BY clause divides the rows by gender.

SELECT gender, SUM(LENGTH(contact_number))
FROM Patients
GROUP BY gender;




SQL Sum Visual Diagram



SQL Sum Labs

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top