what is union in sql 1

what is union in sql

-- UNION: distinct values (slower)
SELECT emp_name AS name from employees
UNION       
SELECT cust_name AS name from customers;

-- UNION ALL: keeps duplicates (faster)
SELECT emp_name AS name from employees
UNION ALL      
SELECT cust_name AS name from customers;

Here is what the above code is Doing:
1. The first query selects all the employee names from the employees table.
2. The second query selects all the customer names from the customers table.
3. The UNION operator combines the results of the two queries and returns only distinct values.
4. The UNION ALL operator combines the results of the two queries and returns all values.

Similar Posts