MYSQL: UNION – tutorial with examples
source link: https://thispointer.com/mysql-union-tutorial-with-examples/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
MYSQL: UNION EXAMPLE
Before we look into the real-world example from MySQL workbench, let us focus on a small concept to make things clear.
Let’s say we have Table1 and Table2 with some data.
figure2PRO TIP : UNION clause adds the results vertically.
See the UNION RESULTS in figure2, we got five records with duplicates removed, and row with data <2,R> is the common data result set.
We were moving on to a real-world example now. In the below example, we have two tables carrying the data of two different companies A and B and we will be writing a union query to merge the data of a few columns from both to get the results.
table employees_companyAtable employees_companyBSELECT first_name , emp_category FROM employees_companyA UNION SELECT first_name , category FROM employees_companyB
Output:-
results of union queryThe record that was common has been added only once <Veronika,
Intern>.
Pay attention to the column names as we can see that the second column name ‘emp_category‘ in the results is taken from the first query ( refer table ’employees_companyA’ ). Let us see what happens on reversing the order of queries and running it again.
SELECT first_name , category FROM employees_companyB UNION SELECT first_name , emp_category FROM employees_companyA
Output:-
second column name is categoryAgain the column name ‘category’ has been taken from first select query.
MYSQL: UNION RESTRICTIONS
There are a few points that should be considered when working with the UNION clause in MySQL.
- All the queries used with the UNION clause must have the same number of columns.
- The data type of the columns used in each query should be the same.
- We cannot use the UNION clause in subqueries.
- We cannot use the UNION clause with aggregate functions.
MYSQL: UNION DISTINCT
As discussed above, the UNION clause will return the single set of results for the common records, and hence DISTINCT is an optional keyword to be used with UNION in MySQL.
Putting a DISTINCT keyword will make no difference in the results of the query.
For example, again running the queries with UNION clause, will give us the same output as without DISTINCT keyword.
SELECT first_name , emp_category FROM employees_companyA UNION DISTINCT SELECT first_name , category FROM employees_companyB
Output:-
MYSQL: UNION ALL
ALL is another keyword which can be used with the UNION clause. The difference it makes to the results is that when ALL is applied to the UNION clause in the query, it will additionally return the duplicate results of multiple queries. Let us see with an example:
Again, we are taking the same tables here but with a different set of data in Table employees_companyB. Table employees_companyB has some more records which are same as in Table employees_companyA (see the records from emp_no 8 to 11).
TableA – employees_companyATableB – employees_companyBObserve the query and results in the output.
SELECT first_name , emp_category FROM employees_companyA UNION ALL SELECT first_name , category FROM employees_companyB
Output:-
Results of UNION queryMYSQL: UNION ORDER BY
ORDER BY Clause with UNION operator is used to view the results in a particular order. In the below example we will be viewing the results ordered by first_name.
SELECT first_name , emp_category FROM employees_companyA UNION ALL SELECT first_name , category FROM employees_companyB ORDER BY first_name
Output:-
We hope this article will help you to build a good understanding on MySQL UNION Clause.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK