title: Building a hierarchical tree with a single SQL query
body: <p>I have a <strong>SQL</strong> table with the following structure.</p>
  
  <pre><code>id   - int
  par  - int (relational to id)
  name - varchar
  </code></pre>
  
  <p>Column <strong>par</strong> contains references to <strong>id</strong> or <em>NULL</em> if no reference, this table is meant to build an hierarchical tree.</p>
  
  <p>Then, given the data:</p>
  
  <pre><code>id par  name
  
  1  NULL John
  2  NULL Mario
  3  1    George
  4  3    Alfred
  5  4    Nicole
  6  2    Margaret
  </code></pre>
  
  <p>I want to retrieve a hierarchical tree, up to the last parent, from a given single <strong>id</strong>.</p>
  
  <p>Example, I want to know the tree from <strong>Nicole</strong> to the last parent. So the query result will be:</p>
  
  <pre><code>id par  name
  
  5  4    Nicole
  4  3    Alfred
  3  1    George
  1  NULL John
  </code></pre>
  
  <p>I would normally do this with a SQL query repeating over and over and building the tree server side but I do not want that now.</p>
  
  <p>Is there any way to achieve this with a single SQL query?<br/>
  I need this for either MySQL or PgSQL.</p>
  
  <p>And I want to know also, if possible, is it also widely supported? In which versions of either MySQL or PgSQL can I expect support?</p>