title: How to Create Category Tree Hierarchy with Multiple Levels SQL?
body: <p>I have a table which represents a category hierarchy and the element at top of the hierarchy has the parent id as 0. 
  There are over 54K unique IDs in the CatID Column. Each ID can be a parent of another. The categories go 8 levels deep.
  The table is as the exaple below:</p>
  
  <pre><code>CatID   ParentID  CatName
  1       0         Home
  .       .         .
  .       .         .
  20      1         Vehicles
  .       .         .
  35      20        SUV
  36      20        Motorbikes
  .       .         .
  90      35        BMW
  91      35        Toyota
  .       .         .
  234     91        Land Cruiser
  </code></pre>
  
  <p>And this is the result I would like to achieve:</p>
  
  <pre><code>Cat0   Cat1       Cat2        Cat3    Cat4         Cat5   Cat6   Cat7
  Home   Vehicles   SUV         Toyota  LandCruiser
  Home   Vehicles   SUV         BMW            
  Home   Vehilces   Motorbikes
  .      .           .
  </code></pre>
  
  <p>How can I do this? Do I need some sort of loop to go through all IDs?</p>
  
  <p>There was a <a href="https://stackoverflow.com/questions/18427154/sql-hierarchy-loop-query">similar question</a> asked before, so I've used the same table structure to explain my point, but the answer is not exactly what I'm looking for.</p>
  
  <p>Can anyone help please?</p>