Summary
Summary of SQL Aggregation Lesson
Overview
In this lesson, we are revising the SUM
function in SQL to calculate the total population of cities within the district of California from a table called city
. The table consists of columns: ID
, country code
, district
, and population
.
SQL Query Steps
-
Select Data: Start by selecting data from the
city
table.SELECT * FROM city
-
Filter District: Add a
WHERE
clause to filter the results for the district of California.WHERE district = 'California'
-
Calculate Total Population: Use the
SUM
function to aggregate thepopulation
values for the filtered results.SELECT SUM(population) FROM city WHERE district = 'California'
Conclusion
The final SQL query will sum the population values only for cities in California, allowing us to effectively get the total population for that district. This method works for both DB2 and MySQL databases.
That's it for revising aggregations with the SUM
function!