 <p>It is bad practice to use null as id for your question I would use something like this</p> <pre><code>select * from post p1 where not exists (select 1 from comment c1 where c1.post_id = p1.id) union select * from post p2 where exists (select 1 from comment c1 where c1.post_id = p1.id and c2.user_id &lt;&gt; 124) </code></pre>