what is UNION & UNION ALL ?

The MySQL UNION operator is used to combine the result sets of 2 or more SELECT statements. It removes duplicate rows between the various SELECT statements. Each SELECT statement within the UNION operator must have the same number of fields in the result sets with similar data types.

  • There must be same number of expressions in both SELECT statements.
  • Since the UNION operator by default removes all duplicate rows from the result set, providing the UNION DISTINCT modifier has no effect on the results.
  • The column names from the first SELECT statement in the UNION operator are used as the column names for the result set.
  • Syntax:-

    SELECT expression1, expression2, ... expression_n FROM tables [WHERE conditions] UNION [DISTINCT] SELECT expression1, expression2, ... expression_n FROM tables [WHERE conditions];

    Return single field

    select sid from suppliers select sid from orders

    Using ORDER BY


    UNION ALL Operator

    The MySQL UNION ALL operator is used to combine the result sets of 2 or more SELECT statements. It returns all rows from the query and it does not remove duplicate rows between the various SELECT statements. Each SELECT statement within the MySQL UNION ALL operator must have the same number of fields in the result sets with similar data types.



    order by






    Previous Next


    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here


    Ittutorial