Loading
Lesson 34
Courses / HackerRank SQL Problem Solving Solution Challenges
Japan Population | SQL Aggregation | HackerRank Solution

Summary

Summary of Japan Population Calculation

In this tutorial, we are tasked with calculating the total population of Japanese cities from a SQL database. The relevant table is named city and includes the following columns:

  • ID
  • country code
  • district
  • population

Steps to Query Population

  1. Filtering Cities: We need to filter the cities to only include those from Japan, which is indicated by the country code JPN.

  2. Using the SUM Function: To calculate the total population, we will utilize the SQL SUM function on the population column.

  3. Writing the Query:

    • Start with the base query: SELECT * FROM city
    • Modify it to filter Japanese cities:
      SELECT population 
      FROM city 
      WHERE country code = 'JPN';
      
    • Finally, implement the sum:
      SELECT SUM(population) 
      FROM city 
      WHERE country code = 'JPN';
      

Submitting Code

After constructing the query, the code can be submitted to output the total sum of the population for Japanese cities based on the provided data.

Video Transcript

Hey everybody, I will do Japan population from the aggregation subsection of SQL and hacker rank. So we're given the table city has the columns ID, country code district and population. We're asked to query the sum of populations for all Japanese cities. So if you want to get a Japanese city that is a row for a city, you have to filter by the country code having the value JPN. And then we take all the populations and we're going to use the sum function. So let's get started. We can use DB2 or MySQL. So I'm going to do select star from city and from this we're going to modify this query to have the correct answer. So first we have to do where. So where the country code column has the value is equal to JPN. That way we filter all the rows to only having Japanese cities. And we have to take the column population so select population. Matt, we need to add them up, right? So we're going to do the function sum of population like so. And that's it. I'm going to go submit code. And here's the sum of the population according to this data.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: