title: SQL:Select columns based on rows of another table
body: <p>How do I create a sql select statement where the select column names are values in rows of another table (I am using postgresql)? I have a language table with language codes:</p>
  
  <pre><code>language
  ========
  id    code  name
  -------------------
  1     en    English
  2     fr    French
  3     it    Italian
  
  country
  =======
  id    en    fr    it   other_columns
  ------------------------------------
  1     ...
  2     ...
  </code></pre>
  
  <p>I want to select the id and all the language columns from the country table and they are listed in the language table. Something like:</p>
  
  <pre><code>SELECT id, (SELECT code FROM language) FROM country
  </code></pre>
  
  <p>so I effectively end up with:</p>
  
  <pre><code>SELECT id, en, fr, it from country
  </code></pre>
  
  <p>Thanks!</p>