Skip to content
Finding Out Country's Population By Using Self Joins
  • AI Chat
  • Code
  • Report
  • Spinner

    Comparing a country to itself Self joins are very useful for comparing data from one part of a table with another part of the same table. Here, I am interested in finding out how much the populations for each country changed from 2010 to 2015, and will query this change by performing a self join.

    In this exercise, we'll work to answer this question by joining the populations table with itself.

    Unknown integration
    Queryavailable as
    query
    variable
    SELECT 
    	p1.country_code, 
        p1.size AS size2010, 
        p2.size AS size2015
    FROM world.populations AS p1
    INNER JOIN world.populations AS p2
    ON p1.country_code = p2.country_code
    WHERE p1.year = 2010 AND
    -- Filter such that p1.year is always five years before p2.year
    p2.year = 2015;