title: SQL: How to understand reason for needing to join on 2 columns for 3 tables
body: <p>I am told that the correct way to join these 3 tables, is to join on 2 columns, as demonstrated below:</p>
  <pre><code>SELECT c.code, name, region, e.year, fertility_rate, unemployment_rate  
  FROM countries AS c  
  INNER JOIN populations AS p 
  ON c.code = p.country_code  
  INNER JOIN economies AS e  
  ON p.country_code = e.code AND p.year = e.year
  </code></pre>
  <p>My question relates to the error that occurs when I do it this way, without the &quot;AND p.year = e.year&quot; on the second inner join:</p>
  <pre><code>SELECT c.code, name, region, e.year, fertility_rate, unemployment_rate  
  FROM countries AS c  
  INNER JOIN populations AS p 
  ON c.code = p.country_code  
  INNER JOIN economies AS e  
  ON p.country_code = e.code
  </code></pre>
  <p>The result is:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Code</th>
  <th>Country</th>
  <th>Region</th>
  <th>Year</th>
  <th>Fertility Rate</th>
  <th>unemployment rate</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>AFG</td>
  <td>Afghanistan</td>
  <td>Southern and Central Asia</td>
  <td>2015</td>
  <td>5.746</td>
  <td>null</td>
  </tr>
  <tr>
  <td>AFG</td>
  <td>Afghanistan</td>
  <td>Southern and Central Asia</td>
  <td>2010</td>
  <td>5.746</td>
  <td>null</td>
  </tr>
  <tr>
  <td>AFG</td>
  <td>Afghanistan</td>
  <td>Southern and Central Asia</td>
  <td>2015</td>
  <td>4.653</td>
  <td>null</td>
  </tr>
  <tr>
  <td>AFG</td>
  <td>Afghanistan</td>
  <td>Southern and Central Asia</td>
  <td>2010</td>
  <td>4.653</td>
  <td>null</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>My question is this: Why am I getting repeats of the years 2010 and 2015 with the 2 different values for fertility rate? The tutorial says &quot;The trouble with doing your last join on c.code = e.code and not also including year is that e.g. the 2010 value for fertility_rate is also paired with the 2015 value for unemployment_rate.&quot;</p>
  <p>I don't understand the explanation and therefore I don't understand conceptually what is going wrong here...</p>
  <p>Can someone walk me through the steps SQL is taking?</p>