1-- UNION: distinct values (slower)
2SELECT emp_name AS name from employees
3UNION
4SELECT cust_name AS name from customers;
5
6-- UNION ALL: keeps duplicates (faster)
7SELECT emp_name AS name from employees
8UNION ALL
9SELECT cust_name AS name from customers;
1SELECT expression1, expression2, ... expression_n
2FROM tables
3[WHERE conditions]
4UNION ALL
5SELECT expression1, expression2, ... expression_n
6FROM tables
7[WHERE conditions];
1Combines the results from 2 or more SELECT statements and returns only
2distinct values.
3Example: Returns the cities from the events and subscribers tables.
4SELECT city FROM events
5UNION
6SELECT city from subscribers;
1SQL> SELECT ID, NAME, AMOUNT, DATE
2 FROM CUSTOMERS
3 LEFT JOIN ORDERS
4 ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID
5UNION
6 SELECT ID, NAME, AMOUNT, DATE
7 FROM CUSTOMERS
8 RIGHT JOIN ORDERS
9 ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
1UNION:
2COMBINES THE RESULT OF 2 QUERY AND
3REMOVES DUPLICATE ROWS AND
4SORTS BY FIRST COLUMN