title: SQL 'DELETE CASCADE'
body: <p>I have a table B that has a foreign key to table A, and now I want to do a sorta "DELETE CASCADE" thingie on A, but PostgreSQL won't accept the following:</p>
  
  <pre><code>DELETE FROM ATable WHERE aid IN
  (
      DELETE FROM BTable WHERE aid IN
      (
          ... [expression that depends on the entries in BTAble] ...
      )
      RETURNING aid
  );
  </code></pre>
  
  <p>Seems that only SELECT can be inside the <code>IN ()</code> clause. I suppose there is some easy (and standard SQL, not PostgreSQL-specific?) way of doing this?</p>
  
  <p><strong>Edit</strong>: is it safe to say that something is badly structured when you bump into this kind of problem? In our case I have a gut feeling that the hits in <code>..[expr]..</code> should be in a new CTAble instead of as a subset in ATable, but I can't really point to any design paradigm to support that.</p>