title: Rewrite union query to a single query
body: <p>I wondered if it was possible to write this as one query:</p>
  <pre><code>SELECT * FROM
  (SELECT  &quot;markets&quot;.* 
    FROM &quot;markets&quot; 
    INNER JOIN &quot;positions&quot; ON &quot;positions&quot;.&quot;id&quot; = &quot;markets&quot;.&quot;position_id&quot; 
    INNER JOIN &quot;players&quot; ON &quot;players&quot;.&quot;id&quot; = &quot;positions&quot;.&quot;player_id&quot; 
    INNER JOIN &quot;other_players&quot; ON &quot;other_players&quot;.&quot;id&quot; = &quot;players&quot;.&quot;other_player_id&quot; 
    WHERE (markets.updated_at &gt; '2021-01-10 11:50:14.136015') 
    AND &quot;markets&quot;.&quot;on_feed&quot; = true
    AND &quot;markets&quot;.&quot;deleted_at&quot; IS NULL
  
    UNION
  
    SELECT  &quot;markets&quot;.* 
    FROM &quot;markets&quot; 
    WHERE (markets.updated_at &gt; '2021-01-10 11:50:14.136015') 
    AND &quot;markets&quot;.&quot;on_feed&quot; = true 
    AND &quot;markets&quot;.&quot;deleted_at&quot; IS NOT NULL) results
  ORDER BY results.updated_at DESC
  </code></pre>
  <p>There's a lot of overlap in the two queries. In the first one we want all markets that have those associations in place. For the 2nd query we don't really care if the associations are there or not.</p>