title: Multi level SQL join
body: <p>I have 3 different tables names as state, config, and user</p>
  <pre><code>state: columns
  --------------
  taskid
  class
  state
  queue time
  running time
  
  config: columns
  ---------------
  class
  confname
  
  user: columns
  -------------
  username
  userid
  </code></pre>
  <p>I can join the <code>state</code> and <code>config</code> tables using <strong>class</strong> column, but in my result set, I want to add the username as well, but there is not matching columns to join. But I can do this via inclusing one more table <code>tasks</code>  that has a relationship with the <code>state</code> table.</p>
  <pre><code>tasks: columns
  --------------
  taskid,
  userid,
  taskname
  </code></pre>
  <p>Using this table, I want to generate the output columns like below.</p>
  <pre><code>username - from users table
  confname - from config table
  state, queue time, running time - from the state table
  </code></pre>
  <p>I tried the following query, but its giving zero results. Can someone help me with the better logic?</p>
  <pre class="lang-sql prettyprint-override"><code>select
  p.username,
  c.confname,
  s.state,
  s.queue_time/1000000 ||' sec' as queue_time,
  s.exec_time/1000000 ||' sec' as execution_time
  from
      state as s join
      config c on s.class=c.class
       join ((select distinct(userid)as user from taks q join  state s on q.taskid=s.taskid) j
        join 
       user p on j.user=p.username)
  </code></pre>