SQL Tutorial – SUM() – Summing values in a column.

You want to compute the Sum of all the values in a column or want to compute the sum of all the values for each groups in a table. We have a country table here, and you want to compute the total population of the world or want to compute the total population for each continent.

Solution –

To sum all the values in a column, we use the SUM() function in SQL.

SELECT
    SUM(Population) as total population
FROM
    country

To calculate the sum of all the values in a column for each group, we first group the data by GROUP BY clause and then apply the SUM() function.

SELECT
    Continent,
    SUM(Population) as total population
FROM
    country
GROUP BY
    Continent

Here, we do not have the population data for the Antarctica that is why it is 0.

If you like this post then please share it with others and subscribe to our blog below to learn data science.

Rating: 1 out of 5.

Leave a Reply