title: What is the difference between group by, distinct, Union for selecting distinct values for multiple columns?
body: <p><a href="https://stackoverflow.com/questions/546804/select-distinct-from-multiple-fields-using-sql">This question </a>explained about a way of getting distinct combination of multiple columns. But I want to know the difference between the methods of DISTINCT, UNION, GROUP BY keyword method for this purpose. I am getting different results when using them.
  My queries are like this
  Query 1.<p></p>
  
  <pre><code>select 
  column1,
  column2,
  column3
  from table
  group by 1,2,3
  </code></pre>
  
  <p>Query 2.<p></p>
  
  <pre><code>select distinct 
  column1,
  column2,
  column3
  from table
  </code></pre>
  
  <p>Query 3.<p></p>
  
  <pre><code>SELECT DISTINCT(ans) FROM (
      SELECT column1 AS ans FROM sametable
      UNION
      SELECT column2 AS ans FROM sametable
      UNION
      SELECT column3 AS ans FROM sametable
  ) AS Temp
  </code></pre>
  
  <p>I am getting different number of rows for above queries(Edit: The first two are giving equal number of rows but last one is giving differnetly). Can any body explain what the above queries are doing? Especially the third one?
  <b><p>EDIT:</b> Note that I am doing UNION on same table. In that case what will happen?</p>