title: Using an Alias column in the where clause in Postgresql
body: <p>I have a query like this:</p>
  
  <pre><code>SELECT
      jobs.*, 
      (
          CASE
              WHEN lead_informations.state IS NOT NULL THEN lead_informations.state
              ELSE 'NEW'
          END
      ) AS lead_state
  FROM
      jobs
      LEFT JOIN lead_informations ON
          lead_informations.job_id = jobs.id
          AND
          lead_informations.mechanic_id = 3
  WHERE
      lead_state = 'NEW'
  </code></pre>
  
  <p>Which gives the following error:</p>
  
  <pre><code>PGError: ERROR:  column "lead_state" does not exist
  LINE 1: ...s.id AND lead_informations.mechanic_id = 3 WHERE (lead_state...
  </code></pre>
  
  <p>In MySql this is valid, but apparently not in Postgresql. From what I can gather, the reason is that the <code>SELECT</code> part of the query is evaluated later than the <code>WHERE</code> part. Is there a common workaround for this problem?</p>