title: PostgreSQL does not accept column alias in WHERE clause
body: <p>In this <a href="https://pgexercises.com/questions/joins/threejoin2.html" rel="noreferrer">pgexercises</a> about joining 3 different tables, the answer is given as following:</p>
  
  <pre><code>select mems.firstname || ' ' || mems.surname as member, 
      facs.name as facility, 
      case 
          when mems.memid = 0 then
              bks.slots*facs.guestcost
          else
              bks.slots*facs.membercost
      end as cost
          from
                  cd.members mems                
                  inner join cd.bookings bks
                          on mems.memid = bks.memid
                  inner join cd.facilities facs
                          on bks.facid = facs.facid
          where
          bks.starttime &gt;= '2012-09-14' and 
          bks.starttime &lt; '2012-09-15' and (
              (mems.memid = 0 and bks.slots*facs.guestcost &gt; 30) or
              (mems.memid != 0 and bks.slots*facs.membercost &gt; 30)
          )
  order by cost desc;
  </code></pre>
  
  <p>Why can't I refer to the <code>cost</code> alias in the <code>SELECT</code> list in the <code>WHERE</code> clause?<br>
  If I run the same query with:</p>
  
  <pre><code>        ...
          where
          bks.starttime &gt;= '2012-09-14' and 
          bks.starttime &lt; '2012-09-15' and
          cost &gt; 30
  order by cost desc;
  </code></pre>
  
  <p>an error occurs:</p>
  
  <blockquote>
  <pre><code>ERROR: column "cost" does not exist
  </code></pre>
  </blockquote>
  
  <p>It's clear with me from <a href="https://stackoverflow.com/questions/12034849/accessing-column-alias-in-postgresql#comment16067732_12034878">this answer</a> that it's because of the order of evaluation. But why <code>order by cost desc;</code> is allowed?</p>