Geek Slack

My SQL Tutorial


    MySQL UNION Operator


    MySQL UNION Operator

    The UNION operator in MySQL is used to combine the result sets of two or more SELECT statements into a single result set.

    Example:

    SELECT employee_id, employee_name
    FROM employees
    WHERE department_id = 1
    UNION
    SELECT employee_id, employee_name
    FROM employees
    WHERE department_id = 2;

    This query retrieves the employee IDs and names from the employees table for employees in department 1 and department 2, combining the results into a single result set.

    The UNION operator removes duplicate rows by default. If you want to include duplicate rows, you can use the UNION ALL operator.