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. Suppose you are interested in finding out how much the populations for each country changed from 2010 to 2015. You can visualize this change by performing a self join.
In this exercise, you'll work to answer this question by joining the populations
table with itself. Recall that, with self joins, tables must be aliased. Use this as an opportunity to practice your aliasing!
Since you'll be joining the populations
table to itself, you can alias populations
first as p1
and again as p2
. This is good practice whenever you are aliasing tables with the same first letter.
This exercise is part of the course
Joining Data in SQL
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
-- Select aliased fields from populations as p1
___
-- Join populations as p1 to itself, alias as p2, on country code
___