MySQL Tutorial

MySQL Tutorial MySQL Features MySQL Database Introduction MySQL Environmental Setup MySQL Data Types MySQL variable MySQL Advance table Query MySQL database queries MySQL Entity-Relationship Model MySQL Table Query MySQL Operators MySQL logical conditions MySQL Queries MySQL Clauses Clustered vs Non-Clustered Index MySQL Full text index MySQL Descending Index MySQL Invisible Index MySQL Composite Index MySQL Prefix index MySQL Index MySQL Create index MySQL Drop Index MySQL Show index MySQL Unique index MySQL Table MySQL Variable MySQL View MySQL Constraints MySQL Command Line Client Basic Queries MySQL Stored Procedure MySQL IF Statement MySQL Subquery MySQL Triggers

MySQL Join

MySQL Join MySQL CROSS JOIN MySQL DELETE JOIN MySQL EQUI JOIN MySQL INNER JOIN MySQL Union MySQL NATURAL JOIN MySQL RIGHT JOIN MySQL SELF JOIN MySQL UPDATE JOIN

MySQL Function

MySQL Function MySQL AVG() Function MySQL SUM() Function MySQL String() Function MySQL Advance() Function MySQL Aggregate() Function MySQL COALESCE() Function MySQL Control Flow Function MySQL COUNT() Function MySQL Date And Time Function MySQL GREATEST() Function MySQL ISNULL() Function MySQL LEAST() Function MySQL Math() Function MySQL MAX() Function MySQL MIN() Function MySQL find_in_set() function MySQL ASIN() Function MySQL CEIL() function MySQL CEILING() function MySQL TAN() Function MySQL Truncate() Function MySQL FLOOR() function MySQL LN() function MySQL LOG2() function MySQL LOG10() function MySQL MOD() function MySQL PI() function MySQL POW() function MySQL RADIANS() function MySQL RAND() function MySQL ROUND() function MySQL Character Length Function MySQL Current Date Function MySQL Date Add Function MySQL Date Format Function MySQL Datediff Function MySQL Day Function MySQL Elt Function MySQL Export Set Function MySQL Field Function MySQL Format Function MySQL From Base64 Function MySQL Hex Function MySQL Insert Function MySQL Instr Function MySQL Length Function MySQL CONCAT() function MySQL FIND_IN_SET() function MySQL LIKE() function MySQL LOAD_FILE() function MySQL LOCATE() function MySQL LOG() function MySQL MONTHNAME() function MySQL NOW() function MySQL PERIOD_ADD() function MySQL PERIOD_DIFF() function MySQL POWER() function MySQL QUARTER() function MySQL REVERSE() function MySQL RIGHT() Function MySQL RPAD() function MySQL RTRIM() function MySQL SEC_TO_TIME() function MySQL SOUNDEX() function

Questions

Which Clause is Similar to Having Clause in MySQL

Misc

MySQL Error 1046 - No Database Selected Failed to Start MySQL Service Unit MySQL Service Unit not Found Import MySQL Connector Mudule not Found Error No Module Named MySQL Joins Available in MySQL MySQL Docs MySQL Download For Windows 7 64 Bit MySQL Error Code 1064 MySQL Export MySQL History MySQL Host MySQL Import MySQL Drop All Tables MySQL Drop MySQL Error Code 1175 MySQL Events MySQL Except MYSQL Foreign Key Constraint MySQL If Exists MySQL IndexOf MySQL List All Tables json_extract in MySQL TIMESTAMPDIFF in MySQL MySQL Syntax Checker Sudo MySQL Secure Installation

MySQL Union

MySQL contains the same categories of data in a different table. Sometimes, you require multiple tables to collect data together. MySQL union is a function to combine two table's data on a single page.The union in MySQL removes duplicate rows from the tables and returns unique data in a result set. The union displays the output of more than one table. MySQL union working procedure starts with the SELECT statement. The "UNION" keyword requires at least two tables for their combination. You can also select a column list from several tables.

When the MySQL union shows the entire row's of the two tables in a single set, it is called a "UNION ALL" command. This command works with several conditions and clauses in the MySQL query. The "UNION ALL" does not remove duplicate data from the table.

Rules of the MySQL Union

  • The union must need the same name and number of the column of the table.
  • The column list of the tables must be in the same order for the union query.
  • The table columns have to be similar for the combined similar type of data on one page.
  • The SELECT and UNION statements are necessary to display table output.
  • MySQL clauses use for showing output as per the required condition.

Syntax of the MySQL Union function

MySQL union for two table's syntax shows below.

SELECT column_1, column_2 FROM first_table
UNION
SELECT column_1, column_2 FROM second_table;

Syntax of the multiple MySQL Union function

MySQL union for two table's syntax shows below.

SELECT column_1, column_2 FROM first_table
UNION
SELECT column_1, column_2 FROM second_table
UNION
SELECT column_1, column_2 FROM third_table;

Syntax of the MySQL Union ALL function

MySQL union for two table's syntax shows below.

SELECT column_1, column_2 FROM first_table
UNION ALL
SELECT column_1, column_2 FROM second_table;

The setting of the MySQL Union

  • Creates the first table with required columns and their data type.
  • Insert values in the first table as per requirement.
  • Show the first table's data using the output query.
  • Create the second table with required columns and their data type.
  • Insert values in the second table as per requirement.
  • Show the second table's data using the output query.

Examples of the UNION and UNION ALL

Example1:In this example, you can use a single union function for combining two tables. This function uses technology and a study table. Here, both table has same column name and its data types.

mysql> SELECT department_id, name, admissions FROM Technology
UNION
SELECT department_id, name, admissions FROM STUDY;

Output 

MySQL Union

This output shows combine information of the technology and study tables. The first four rows represent the technology table. The last three rows represent the study table. You get three columns and seven rows using the union function.

Example2: This example uses multiple union functions for combining more than two tables. You can combine more than two tables using the union function and removes duplicate functions. Here, the example combines technology, study, and arts tables.

mysql> SELECT department_id, name, admissions FROM Technology
UNION
SELECT department_id, name, admissions FROM STUDY
UNION
SELECT department_id, name, admissions FROM ARTS;

Output 

MySQL Union

The above image displays eleven rows and three columns of the three tables. You can see table data as per the table's position with the union function. This function removes duplicate information and displays the original value of the tables. The first table, which is technology, contains the first few rows of the table.

Example3: This example uses the ORDER BY clause with the union function. This clause comes with the "ASC" keyword for ascending order. The "name" columns display in the required order.

mysql> SELECT department_id, name, admissions FROM Technology
UNION
SELECT department_id, name, admissions FROM STUDY
ORDER BY name ASC;

Output 

MySQL Union

This output image returns table information in the ascending order of the name column. This clause applies to the two tables simultaneously and combines information from the first alphabet to the last alphabet.

Example4:MySQL "union all" query works with a required column of the table. This function gives three columns of the technology and study tables. You do not need any clause and condition for the combined two table’s information.

mysql> SELECT department_id, name, admissions FROM Technology
UNION ALL
SELECT department_id, name, admissions FROM STUDY;

Output 

MySQL Union

The "union all" function's output returns all information of the given table. The above image returns all similar and different value contains in the table.

Example5:Execute the following query to get the working procedure of the union function. This function works on an entire table of the database. It returns unique and combined information of the tables.

mysql> SELECT * FROM Technology
UNION 
SELECT * FROM STUDY;

Output

MySQL Union

You can able to display different information using the union function. The first table shows all information but the second table shows only three rows of the table. The second table removes the last row, similar to the first table.

Example6: This example works with multiple "union all" functions in the MySQL query. The union all function combines more than two tables in one table.

mysql> SELECT department_id, name, admissions FROM Technology
UNION ALL
SELECT department_id, name, admissions FROM STUDY
UNION ALL
SELECT department_id, name, admissions FROM ART;

Output

MySQL Union

Here, you can see three different table's data in one image. You can categories the different tables using department id columns. The output image returns the entire information of the technology, arts, and study tables.

Example7: This example works with the "union all" function and the "order by" clause without specifying any order.

mysql> SELECT department_id, name, admissions FROM Technology
UNION ALL
SELECT department_id, name, admissions FROM STUDY
ORDER BY name;

Output 

MySQL Union

The union all function returns table data with an order of the name column. This order by clause sets ascending by default.

Example8:The "union all" function applies with the select all (*) columns of the table. This function works on the entire table format and data.

mysql> SELECT * FROM technology 
UNION ALL
SELECT * FROM study;

Output

MySQL Union

Differences between UNION and JOIN in MySQL

The following table shows similarities and differences between UNION and JOIN.

UNIONJOIN
The union combines the result set of the multiple tables.The join combines the many tables' data using conditions.
MySQL union combines multiple table data into a new row of the table.MySQL join combines multiple table data into a new row of the table.
The selected columns of the table are the same.The selected columns of the table are similar or different.
The union removes duplicate data and display distinct data.The join displays distinct and duplicate data.

Differences between UNION and UNION ALL

The following table shows similarities and differences between UNION and UNION ALL.

UNIONUNION ALL
The union combines the result set of the multiple tables.The union all combines the result set of the multiple tables.
The union removes duplicate data from the tables.The union ALL removes duplicate data from the tables.
The union is lower than UNION ALL function.The UNION ALL is higher than the UNION function.