Loading
Lesson 31
Courses / HackerRank SQL Problem Solving Solution Challenges
Revising Aggregations - The Sum Function | SQL Aggregation | HackerRank Solution

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

  1. Select Data: Start by selecting data from the city table.

    SELECT * FROM city
    
  2. Filter District: Add a WHERE clause to filter the results for the district of California.

    WHERE district = 'California'
    
  3. Calculate Total Population: Use the SUM function to aggregate the population 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!

Video Transcript

Hey everybody, welcome to another lesson. I'll be doing revising aggregations, the sum function from the aggregation subsection of SQL and hacker rank. So we're given the stable city has the column ID named country code district and population. We're asked to pair with the total population of all cities where a district is California. So basically we have a bunch of rows and those rows have population column that has a number. So we have to take all those numbers and add them up except we can only take the rows that have the district value California. So this solution will work for DB2 and my SQL. Let's go. So we're gonna first do I just select star from city and then we're gonna add on to this. So we want to query the population where district is. Let's do where district is California. So we're gonna filter, we're gonna add a where and district is the column and that value has to be equal to California like this. Now what column do we query here? Well, we query the total population. So we have to sum all the values for the population column. So we got to say sum of and then you put population. That's the column name here. So this is gonna take the value of each population column, add them up and bring you the sum and we have a filter here for only the rows that are from California. And there you go. You're gonna collect submit code and that's revising aggregations the sum function.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: