title: PostgreSQL - Select items with certain tags, but keep all tags in the result
body: <p>I have a problem with my tag search where the database correctly returns the images containing the tags, BUT all of the other tags are stripped. I store all of the tags in a separate table &quot;tag map&quot;.</p>
  <p>This is my result for search &quot;tag1&quot; (image actually has more tags than just tag1):</p>
  <pre class="lang-json prettyprint-override"><code>[{&quot;postID&quot;: 1, &quot;images&quot;: {&quot;imageID&quot;: 1, &quot;tags&quot;: [&quot;tag1&quot;]}}]
  </code></pre>
  <p>This is the result that I want for search &quot;tag1&quot;:</p>
  <pre class="lang-json prettyprint-override"><code>[{&quot;postID&quot;: 1, &quot;images&quot;: {&quot;imageID&quot;: 1, &quot;tags&quot;: [&quot;tag1&quot;, &quot;tag2&quot;, &quot;tag3&quot;]}}]
  </code></pre>
  <p>This is the query that generates the first result:</p>
  <pre class="lang-sql prettyprint-override"><code>WITH image_tags AS (
    SELECT images.&quot;post id&quot;, json_build_object (
      'imageID', images.&quot;image id&quot;,
      'tags', json_agg(&quot;tag map&quot;.tag)
    ) AS image,
    FROM images
    JOIN &quot;tag map&quot; ON images.&quot;image id&quot; = &quot;tag map&quot;.&quot;image id&quot;
    WHERE &quot;tag map&quot;.tag = ALL ($1)
    GROUP BY images.&quot;post id&quot;, images.&quot;image id&quot;
  )
  SELECT posts.&quot;post id&quot; AS &quot;postID&quot;, json_agg(image_tags.image) AS images 
  FROM posts
  JOIN image_tags ON posts.&quot;post id&quot; = image_tags.&quot;post id&quot;
  GROUP BY posts.&quot;post id&quot;
  </code></pre>