title: Self join for Hierarchy in a table
body: <p>I have a table called <code>hierarchy</code> that has <code>childid</code> and <code>parentid</code> columns like below:</p>
  <pre class="lang-none prettyprint-override"><code>childid  parentid
  53047    52915
  52915    52914
  52914    53037
  52914    91856
  53037    161212
  91856    161212 (great great grandparent of 53047)
  </code></pre>
  <p>I have another table called <code>Names</code> that has a <code>location_id</code> and a <code>name</code> column. <code>location_id</code> has all the values that are in <code>childid</code> and <code>parentid</code> from the <code>hierarchy</code> table.</p>
  <p>Now I want to get all the parent, grandparent and great-grandparent names for <code>childid</code> 53047. My output should look like below:</p>
  <pre class="lang-none prettyprint-override"><code>childid childname   parentname grandparentname greatgrandparentname
  53047   Strasbourg  Bas-Rhin   Alsace          France
  </code></pre>