UNION clause | SQL Tutorial and Query Example

Text copied!

UNION clause


  • UNION clause :

    UNION clause is used to combine/merge the result of multiple SELECT statements into a single result by eliminating duplicate rows.

    The syntax of the UNION clause generally looks like this :
    Here's an example of how you might use the UNION clause :

    Let's suppose we have two tables :

    SELECT column_name(s)
    FROM table1
    UNION
    SELECT column_name(s)
    FROM table2;
            
    UNION clause
    SELECT *
    FROM [Employees]
    UNION
    SELECT *
    FROM [Employees_2];
            
    UNION clause
    SELECT Employee_Id, Employee_Name, Gender, Salary
    FROM [Employees]
    UNION
    SELECT Employee_Id, Employee_Name, Gender, Salary
    FROM [Employees_2];
            
    UNION clause

    Frequently Asked Questions :

    A union in SQL combines the results of two or more SELECT queries into a single result set.
    A UNION clause combines rows from different tables into a single result set, while a JOIN clause combines columns from different tables based on a related column.
    The UNION clause is used to merge the results of multiple SELECT statements into a single result set.
    Rules for using UNION include ensuring that the number of columns, their data types, and their order match between the SELECT statements being combined.