WITH ... AS statement not working as expected I get this error:



It arises when I try to execute the following query:

with expensive_service as (
    select s1.*
    from service s1, service s2
    where s1.price &gt; s2.price
)
select * 
from service except expensive_service;


I was trying to implement WITH ... AS (link to PostgreSQL docs). 

This query gives me the desired output I'm looking for:

select * 
from service except ( 
    select s1.*
    from service s1, service s2
    where s1.price &gt; s2.price
)


Any aid directing me where the error lies would be greatly appreciated!
The CTE (common table expression) is like a view and you need to treat it like any other view (or table). You need a complete SELECT statement as the second part of the EXCEPT: 

with expensive_service as (
    select s1.*
    from service s1, service s2
    where s1.price &gt; s2.price
)
select * 
from service 
except 
select *
from expensive_service;




Note that parentheses around the individual parts of a UNION, EXCEPT or INTERSECT are not needed (unless you need an ORDER BY for only that query, e.g. because you are using a LIMIT clause). 

So your original query could (should in my opinion) also be written as

select * 
from service 
except  
select s1.*
from service s1, service s2
where s1.price &gt; s2.price