UNION ALL operator | SQL Tutorial and Query Example

Text copied!

UNION ALL operator


  • UNION ALL operator :

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

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

    Let's suppose we have two tables :

    To enhance your understanding of 'SQL operators', be sure to check out our next tutorial.
    SELECT column_name(s)
    FROM table1
    UNION ALL
    SELECT column_name(s)
    FROM table2;
            
    UNION ALL operator
    SELECT *
    FROM [Employees]
    UNION ALL
    SELECT *
    FROM [Employees_2];
            
    UNION ALL operator
    SELECT Employee_Id, Employee_Name, Gender, Salary
    FROM [Employees]
    UNION ALL
    SELECT Employee_Id, Employee_Name, Gender, Salary
    FROM [Employees_2];
            
    UNION ALL operator

    Frequently Asked Questions :

    The UNION ALL operator in SQL combines the results of two or more SELECT statements, including duplicates.
    Whether it's good to use UNION ALL in SQL depends on your specific use case and whether you need to retain duplicate rows or not.
    No, UNION ALL does not remove duplicates; it includes all rows from both queries, even if they are duplicates.
    To merge two queries in SQL, use the UNION ALL operator if you want to include duplicates or UNION if you want to remove duplicates.