How to join three table using inner join in postgres sql
You can't test for null using = or &lt;&gt; (or !=) 

To test for non-null values you need to use IS NOT NULL:

where comp.templateid is not null