title: SQL – Eliminating duplicates in UNION ALL using a WHERE clause
body: <p>On toptal.com I found this question to which they provided the following solution:</p>
  
  <pre><code>SELECT * FROM mytable WHERE a=X UNION ALL SELECT * FROM mytable WHERE b=Y AND a!=X
  </code></pre>
  
  <p>Can someone explain the usage of X and Y here? Are these variables? Does it also work this way in MySQL? I can't seem to get this kind of query running on a test server.</p>
  
  <p>Also, they make the following claim:</p>
  
  <blockquote>
    <p>The key is the AND a!=X part. This gives you the benefits of the UNION (a.k.a., UNION DISTINCT) command, while avoiding much of its performance hit.</p>
  </blockquote>
  
  <p>But if this were true, why would anyone ever use UNION DISTINCT? In particular, why wouldn't it be implemented using this supposedly more efficient way?</p>