The Wayback Machine - https://web.archive.org/web/20240402062144/https://www.geeksforgeeks.org/sql-union-all/
Open In App

SQL UNION ALL

Improve
Improve
Like Article
Like
Save
Share
Report

In SQL sometimes there is a need to obtain combined records of two or more SELECT statements from two or more than two tables. The UNION ALL command helps us achieve the same task as it is used to combine the results of two or more SELECT statements. The UNION ALL command also includes the duplicate records from the SELECT statements and it is different from the UNION command which does not include duplicate records both are the same. For performing the UNION ALL operation, it is necessary that both the SELECT statements should have an equal number of columns otherwise the resulting expression will result in an error.

Syntax:

SELECT columns FROM table1

UNION ALL

SELECT columns FROM table2;

How to use SQL UNION ALL?

To perform the SQL UNION ALL operation on two or more tables kindly follow the below-mentioned steps :

Step 1: Creating a new Database

To start with first we need to create a database and to create a database we use CREATE DATABASE command. As an example we are creating a database GeekForGeeks.

Query:

CREATE DATABASE GeeksForGeeks;

Output

Creating new database

Creating new database

Step 2: Selecting the Database

After creating the database we now need to select or use it and for that purpose we use the USE command. So now we will select our database GeeksForGeeks.

Query:

USE GeeksForGeeks;

Output

Selecting the Database

Selecting the database

Step 3: Creating two Tables

As we have selected our database now we will create two tables in our database. To create a table we use CREATE TABLE command. As an example we are creating two tables Students and Trip_Detail and then we will merge the record of both tables.

Query:

 CREATE TABLE STUDENTS(
ROLL_NO INT,
NAME VARCHAR(20),
DOB DATE,
AGE INT(2));
 CREATE TABLE TRIP_DETAIL(
ROLL_NO INT,
NAME VARCHAR(20),
DOB DATE,
AGE INT(2));

Output

Creating table Students

Creating table Students

Creating table TripDetail

Creating table TripDetail

Step 4: Inserting data

Now after creating both tables, we will proceed with inserting data in both the tables. We use the INSERT INTO command to insert data in our tables. We will insert some data in both Students and TripDetail tables.

Query:

 INSERT INTO STUDENTS VALUES(1,"DEV SHARMA","2001-08-16",17);
INSERT INTO STUDENTS VALUES(2,"AMAN VERMA","2002-01-04",16);
INSERT INTO STUDENTS VALUES(3,"KRISH VATSA","2000-11-29",18);
 INSERT INTO TRIP_DETAIL VALUES(1,"DEV SHARMA","2001-08-16",17);
INSERT INTO TRIP_DETAIL VALUES(2,"AMAN VERMA","2002-01-04",16);
INSERT INTO TRIP_DETAIL VALUES(3,"KRISH VATSA","2000-11-29",18);
INSERT INTO TRIP_DETAIL VALUES(4,"VARUN GOYAL","2003-09-21",15);

Output

Inserting data in Stuents table

Inserting data in Students table

Inserting data in TripDetail table

Inserting data in TripDetail table

Step -5: Performing UNION ALL operation

After inserting data now we will perform the SQL UNION ALL operation so we will combine the entire records of both the tables i.e. STUDENTS and TRIP_DETAIL and we will notice that the duplicates values will also be returned in the result. We use the UNION ALL command to perform the UNION of all records of both tables.

Query:

   SELECT * FROM STUDENTS
UNION ALL
SELECT * FROM TRIP_DETAIL;

Output

Performing Union all

Performing UNION ALL

Conclusion

In this article, we have learned about the UNION ALL command in SQL. To summarize the article, sometimes we need to obtain records of two or more SELECT statements from two or more than two tables then the UNION ALL command helps us do that as it is used to combine results of two or more SELECT statements from different tables. The UNION ALL command includes the duplicate records from the SELECT statements whereas the UNION command does not include duplicate records otherwise both the commands are same. For performing the UNION ALL operation, it is necessary that both the SELECT statements should have equal number of columns otherwise the resulting expression will result in an error.

FAQs on Union All Operator

Q.1: What is UNION ALL command?

Answer:

UNION ALL command obtains combined records of two or more SELECT statement from two or more than two tables.

Q.2: Is duplicates records allowed in UNION ALL?

Answer:

Yes, duplicates records are allowed in the UNION ALL command but not in UNION command.

Q.3: Is it necessary to have same number of columns in all SELECT queries of UNION ALL?

Answer:

Yes, it necessary to have same number of columns in all SELECT queries otherwise an error will occur.



Last Updated : 13 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads