×

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 SELECT LIKE Operator

The SQL SELECT LIKE Operator tutorial helps us understand how to use the LIKE operator in the SELECT query with examples. The SQL SELECT LIKE Operator retrieves the records from the...

4 minutes read.

SQL FULL JOIN

In this section, we will help you understand the concept of the SQL FULL Join clause with a few examples. The SQL FULL Join query is executed to display the integrated...

3 minutes read.

SQL SELECT AVG

In this tutorial, we will learn about the aggregate function name avg() function concept in SQL with the help of examples. The AVG() function is one of the aggregate functions in...

3 minutes read.

DDL Commands in SQL

DDL stands for Data Definition Language. Data Definition Language is a bunch of SQL commands used to create, modify and delete Database schema but not the records or data. Data Definition...

5 minutes read.

SQL LOGICAL Operator

In this tutorial, we will understand the operator who falls under the logical operator in SQL with the help of examples. The SQL Logical Operator displays the query result in one...

5 minutes read.

SQL Cast Operator

While writing SQL queries, we can see many values which are to be converted into another datatype for accessing them. In SQL this conversion is generally done by CAST function. CAST...

4 minutes read.

SQL Right Join

The SQL Right Join query displays all the table records and similar records from the left table. The query display zero records if it doesn’t find any similar records. If...

4 minutes read.

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...

3 minutes read.

SQL Injection

SQL injection is a technique, this may destroy the database. It is one type of hacking technique. SQL IN WEB PAGES: Injection occurs when we ask for input like an id or...

3 minutes read.

SQL Create Database

This tutorial will help us understand how to CREATE a Database in SQL with a few examples. The first step for storing structured records in the database is creating the databases. We...

4 minutes read.

Difference between Delete, Drop and Truncate in SQL

What is the Delete command in SQL? In DML (Data Manipulation Language), we use the delete command that allows us to delete the some entries and modify the databases in SQL....

4 minutes read.

SQL SELECT Statement

The SQL SELECT statement is used to retrieve the data from the tables. We can also retrieve the selected records from the table using the query condition. The SQL SELECT...

5 minutes read.

Nth highest salary

The most common and important question asked in interviews that how we can find the Nth highest salary in a table (2nd highest salary, 3rd highest salary, or Nth highest...

6 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.

SQL KEYS

SQL KEYS are single or multiple attributes used to get data from the table according to the requirement or condition. They can also be used to set up relationships amongst...

3 minutes read.

SQL SELECT WHERE Clause

In this SQL section, we will help you understand the concept of the SQL SELECT WHERE clause with a few examples. The WHERE clause in SELECT query displays those records as...

5 minutes read.

Update Query in SQL

Update is an SQL command that is used to modify the data that in already present in database. Update is a command of DML. DML means Data Manipulation Language. Basically,...

3 minutes read.

SQL Syntax

In this tutorial, we will help you understand about the different SQL Syntax concepts with the help of an example. Every Structured Query Language starts with Keywords like select, insert, update,...

5 minutes read.

SQL TABLE

SQL TABLE Structured Query Language (SQL) is a relational database (RDBMS) where data is stored in the form of tables, that is, in rows and columns. These tables are known as...

3 minutes read.

How to use COUNT in SQL?

How to use COUNT in SQL Introduction COUNT( ) is an aggregate function in SQL.This function counts the number of records in a table if the condition is not specified.If the condition...

4 minutes read.