×

SQL UNION ALL Operator

This page has all the information about UNION ALL operator, which is used to join all the values from the SELECT queries.

Similar records were not considered in the result in the UNION operator, but UNION ALL considered similar records in the output obtained after the UNION ALL query was executed.

The syntax for UNION ALL Operator is:

SELECT Column_Name_1, Column_Name_2, Column_Name_3, Column_Name_4, Column_Name_5 FROM Table_Name_1 UNION ALL SELECT Column_Name_1, Column_Name_2, Column_Name_3, Column_Name_4, Column_Name_5 FROM Table_Name_2;

Rules to be followed to operate using UNION ALL Operator are as follows:

  1. The number of Columns and Order of Columns must be the same.
  2. The data type must be Compatible.

Let's learn the concept of SQL UNION ALL operator with the help of an example

Consider the already existing table with the following data.

Table Name 1: Diploma_Student

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202111Vaishnavi Patil949188859592916
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Axar Patel858082869284851
202116Meena Mishra787580748577783
202117Mahesh Kumbhar758075788076775
202118Sakshi Patil807874788077782
202119Sopan Bhore706875758080752
202220Prajwal Lokhande808585757880814
202221Anuja Wanare858886828485857
202222Venkatesh Iyer908987909291903
202223Anushka Sen707571748078751
202224Aakash Jain807572748580787
202225Akshay Agarwal858078889082847

Table Name 2: D_Students

Student_IdStudent_NameFirst_SemesterSecond_SemesterThird_SemesterFourth_SemesterFifth_SemesterSixth_SemesterTotalDepartment_Id
202111Vishaka Jain949188859592916
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Aman Sharma858082869284851
202116Meena Mishra787580748577783
202117Manan Vohra758075788076775
202118Sakshi Patil807874788077782
202119Sopan Bhore706875758080752
202220Pratik Rokade808585757880814
202221Ashwini Shewani858886828485857
202222Venkatesh Iyer908987909291903
202223Anushka Sen707571748078751
202224Aakash Jain807572748580787
202225Aman Gupta858078889082847

Example 1: write a query to perform UNION ALL between the Diploma_Student and D_Students tables.

SELECT * FROM Diploma_Student UNION ALL SELECT * FROM D_Students;

We have used two SELECT statements. The First SELECT statement displays the records from the Diploma_Student table performing UNION ALL operation with the records displayed by the Second SELECT statement from the D_Students table in the above UNION ALL operator example.

The output of the above query is as follows:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202111Vaishnavi Patil949188859592916
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Axar Patel858082869284851
202116Meena Mishra787580748577783
202117Mahesh Kumbhar758075788076775
202118Sakshi Patil807874788077782
202119Sopan Bhore706875758080752
202220Prajwal Lokhande808585757880814
202221Anuja Wanare858886828485857
202222Venkatesh Iyer908987909291903
202223Anushka Sen707571748078751
202224Aakash Jain807572748580787
202225Akshay Agarwal858078889082847
202111Vishaka Jain949188859592916
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Aman Sharma858082869284851
202116Meena Mishra787580748577783
202117Manan Vohra758075788076775
202118Sakshi Patil807874788077782
202119Sopan Bhore706875758080752
202220Pratik Rokade808585757880814
202221Ashwini Shewani858886828485857
202222Venkatesh Iyer908987909291903
202223Anushka Sen707571748078751
202224Aakash Jain807572748580787
202225Aman Gupta858078889082847
SQL UNION ALL Operator

Example 2: write a query to perform UNION ALL between the Diploma_Student and D_Students tables ORDER BY Department Id.

SELECT * FROM Diploma_Student UNION ALL SELECT * FROM D_Students ORDER BY Department_Id;

We have used two SELECT statements. The First SELECT statement displays the records from the Diploma_Student table performing UNION ALL operation with the records displayed by the Second SELECT statement from the D_Students table ORDER BY Department Id in the above UNION ALL operator example.

The output of the above query is as follows:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202115Aman Sharma858082869284851
202223Anushka Sen707571748078751
202115Axar Patel858082869284851
202223Anushka Sen707571748078751
202118Sakshi Patil807874788077782
202112Vaibhav Lokhande859092808582862
202112Vaibhav Lokhande859092808582862
202119Sopan Bhore706875758080752
202118Sakshi Patil807874788077782
202119Sopan Bhore706875758080752
202113Yash Dhull908894878590893
202222Venkatesh Iyer908987909291903
202116Meena Mishra787580748577783
202116Meena Mishra787580748577783
202113Yash Dhull908894878590893
202222Venkatesh Iyer908987909291903
202220Pratik Rokade808585757880814
202220Prajwal Lokhande808585757880814
202114Sonali Patole959092889290914
202114Sonali Patole959092889290914
202117Mahesh Kumbhar758075788076775
202117Manan Vohra758075788076775
202111Vishaka Jain949188859592916
202111Vaishnavi Patil949188859592916
202221Ashwini Shewani858886828485857
202225Aman Gupta858078889082847
202221Anuja Wanare858886828485857
202224Aakash Jain807572748580787
202225Akshay Agarwal858078889082847
202224Aakash Jain807572748580787
SQL UNION ALL Operator

Related Topics

SQL ORDER BY

SQL ORDER BY The SQL ORDER BY clause is used to sort the data stored in tables in the database. The sorting can be done in an ascending way, descending way,...

5 minutes read.

SQL Alter Table

In Structured Query Language, if you want to add columns in an existing table, then modify the table, or delete columns from the table. All these operations are allowed only...

7 minutes read.

SQL COPY Table

In this tutorial, we will learn how to copy tables in SQL with the help of examples. Using the SELECT INTO statement in the SQL we can copy one table into...

4 minutes read.

How to create a database in SQL?

How to create a database in SQL It is very necessary to create a database in order to store the data into the database.Database name must always be unique.SQL does not...

6 minutes read.

SQL Tutorial for Beginners

SQL tutorial provides basic and advanced concepts of Structured Query Language and how you deploy SQL to work with a relational database system. Our SQL tutorial is designed for beginners...

3 minutes read.

SQL Data Definition Language

Data definition language directly effects on the structure/schema of the database. CREATE, ALTER, DROP are the commands of DDL.CREATE: Creates new database, table, or view of table.ALTER: Modifies the database or...

3 minutes read.

Grant Command in SQL

What is DCL (Data Control Language)? Data Control Language (DCL) is a computer programming language with syntax intended to manage access to data kept in databases. It is a part of...

3 minutes read.

Types of SQL JOIN

The SQL JOIN combines one or more than one tables based on their relationship. The SQL JOIN involves a parent table and a child table relationship. There are different types of...

10 minutes read.

SQL VIEW

The SQL VIEW concept helps to hide the difficulty of the records and provides limitations to access to the database. The SQL view is similar to the SQL tables. In SQL...

7 minutes read.

Truncate function in SQL

The TRUNCATE is a numeric function in SQL which truncates the number according to the particular decimal points. Syntax of TRUNCATE Function SELECT TRUNCATE(X, D) AS Alias_Name; In the TRUNCATE syntax, X...

4 minutes read.

SQL Primary Key

A field, which contains unique data in a table, is called a PRIMARY KEY (PK). It means, a PRIMARY KEY field must contain unique data in the table. A PRIMARY KEY...

4 minutes read.

SQL DELETE

In this tutorial, you will learn about the SQL DELETE concept by using examples. In Structured Query Language (SQL), we fetch the data from the database table using SELECT Statement and...

3 minutes read.

WHERE Clause vs HAVING Clause

The WHERE Clause and HAVING clause filter the records in the Structured Query Language queries. The main difference between the WHERE clause and the HAVING clause is the WHERE clause...

5 minutes read.

SQL SELECT OR Operator

This SQL tutorial explains and helps us understand how to use the OR operator in the SELECT query with examples. The OR operator in the Structured Query Language is used to...

3 minutes read.

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.

SQL CROSS Join

In this tutorial, we will help you understand the concept of the SQL CROSS Join clause with the few examples. The SQL CROSS Join query is used to join one or...

5 minutes read.

How to use the BETWEEN operator in SQL

In this entire SQL article, we will understand and learn about the BETWEEN operator concept and how to use it in SQL. What is the BETWEEN operator in SQL? The Between operator...

4 minutes read.

SQL SELECT IN

SQL SELECT IN is a logical operator in Structured Query Language. It is used in SQL queries to reduce the use of multiple 'OR' operators. s The IN operator in SQL...

6 minutes read.

GROUP BY vs ORDER BY

The GROUP BY clause and ORDER BY clause in SQL are used to arrange data obtained by SQL queries. The important difference between the GROUP BY clause and ORDER BY...

4 minutes read.

How to drop a column in SQL?

How to drop a column in SQL Introduction To delete a column from an already created table, one needs to use the ALTER command along with the DROP COLUMN clause. Syntax: ALTER TABLE tablename...

4 minutes read.