Difference between UNION and UNION ALL Commands

UNION : UNION Command is used to select the related information from two tables.When using the UNION command , the selected columns need to be of same datataype.with UNION Command only the distinct values are selected.
UNION ALL : UNION ALL Command is equal to the UNION Command except the UNION ALL select all the values.
   
    ex:
       
    Table1  
       
         id    rank
         1    First
         2    Second
         3    Third
         4    Fourth

    Table2  
       
         id    rank
         1    First
         2    Second
         3    Seventh
         4    Tenth

     UNION:
     select rank from Table1  UNION select rank from table2

      result:

          First
          Second
          Third
          Fourth
          Seventh
          Tenth

     UNION ALL:
     select rank from Table1  UNION ALL select rank from table2

     result:

             First
             Second
             Third
             Fourth
             First
             Second
             Seventh
             Tenth


No comments:

Post a Comment