This command combines results from two or more select queries. In order to use the UNION command, you must have the same number of columns in each select statement and the corresponding columns must have the same data type.

Note: Union removes duplicate rows (unless UNION ALL is used)

Combining entire customer e-mail list from two separate stores (note: each customer will be listed only once, even if they signed up for e-mails in both stores):

SELECT customer_id, email FROM store1
UNION
SELECT customer_id, email FROM store2

Combining sales figures from two different databases (note: UNION ALL is used in case an employee sold the same amount in both quarters:

SELECT employee, amount FROM salesQ1
UNION ALL
SELECT employee, amount FROM salesQ2;

 

 

Categories: SQL

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

SQL

HAVING

This command is used with GROUP BY to filter group rows. It is similar to WHERE, but HAVING applies to the aggregated rows after the GROUP BY command (WHERE applies to individual rows before the Read more…

SQL

GROUP BY

GROUP BY statements use various aggregate functions to combine rows and look at aggregated information. Aggregate functions used below: MIN, MAX, AVG, COUNT, SUM. The calls below are made a hypothetical transaction database with columns Read more…

SQL

Basic SQL Statements

SQL is case-insensitive, but by convention SQL commands are typed in upper case. The examples below return specific data from a hypothetical database named transactions with columns: first_name, last_name, customer_id, and price. Basic commands: SELECT Read more…