
- SQL - Home
- SQL - Roadmap
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL - Comments
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Cheatsheet
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
SQL - UNION vs UNION ALL
SQL UNION and UNION ALL operators combine rows from multiple tables into a single result set. UNION returns only distinct rows, while UNION ALL returns all rows, including duplicates.
For these operators to work, the tables must have the same number of columns with matching data types, though the number of rows can differ. The column names from the first table determine the column names in the result, and matching columns from other tables are merged accordingly.
Difference Between SQL UNION and UNION ALL
The following table shows the basic differences between UNION and UNION ALL operators in SQL:
Features | UNION | UNION ALL |
---|---|---|
Duplicate Rows | Removes duplicate rows from the result set. | Keeps all rows, including duplicates. |
Performance | Slower because it checks and removes duplicates. | Faster because it doesn't check for duplicates |
Result Size | Returns only unique records. | Returns all matching records, including repeated ones. |
Use Case | Use when you need distinct results without duplicates. | Use when you want to keep all the rows including duplicates. |
Sorting | Automatically sorts data to remove duplicates. | Does not sort the data; keeps the original order. |
Syntax | SELECT ... FROM ... UNION SELECT ... FROM ...; |
SELECT ... FROM ... UNION ALL SELECT ... FROM ...; |
SQL UNION Operator
The UNION operator in SQL combines data from multiple tables that are union-compatible, similar to the union operation in relational algebra.
It returns only distinct rows in the result set, automatically eliminating any duplicate records from the combined tables.
Syntax
Following is the syntax of UNION operator in SQL:
SELECT * FROM table1 UNION SELECT * FROM table2;
Example
Let us first create two tables COURSES_PICKED and EXTRA_COURSES_PICKED with the same number of columns having the same data types.
Create table COURSES_PICKED using the following query:
CREATE TABLE COURSES_PICKED( STUDENT_ID INT NOT NULL, STUDENT_NAME VARCHAR(30) NOT NULL, COURSE_NAME VARCHAR(30) NOT NULL );
Insert values into the COURSES_PICKED table with the help of the query given below:
INSERT INTO COURSES_PICKED VALUES (1, 'JOHN', 'ENGLISH'), (2, 'ROBERT', 'COMPUTER SCIENCE'), (3, 'SASHA', 'COMMUNICATIONS'), (4, 'JULIAN', 'MATHEMATICS');
The table will be displayed as shown below:
STUDENT_ID | STUDENT_NAME | COURSE_NAME |
---|---|---|
1 | JOHN | ENGLISH |
2 | ROBERT | COMPUTER SCIENCE |
3 | SASHA | COMMUNICATIONS |
4 | JULIAN | MATHEMATICS |
Now, let us create another table EXTRA_COURSES_PICKED using the following query:
CREATE TABLE EXTRA_COURSES_PICKED( STUDENT_ID INT NOT NULL, STUDENT_NAME VARCHAR(30) NOT NULL, EXTRA_COURSE_NAME VARCHAR(30) NOT NULL );
Following is the query to insert values into the "EXTRA_COURSES_PICKED" table:
INSERT INTO EXTRA_COURSES_PICKED VALUES (1, 'JOHN', 'PHYSICAL EDUCATION'), (2, 'ROBERT', 'GYM'), (3, 'SASHA', 'FILM'), (4, 'JULIAN', 'MATHEMATICS');
The table will be created as shown below:
STUDENT_ID | STUDENT_NAME | COURSES_PICKED |
---|---|---|
1 | JOHN | PHYSICAL EDUCATION |
2 | ROBERT | GYM |
3 | SASHA | FILM |
4 | JULIAN | MATHEMATICS |
Now, let us combine both of these tables using the UNION query as follows:
SELECT * FROM COURSES_PICKED UNION SELECT * FROM EXTRA_COURSES_PICKED;
The resultant table obtained after performing the UNION operation is as follows:
STUDENT_ID | STUDENT_NAME | COURSE_NAME |
---|---|---|
1 | JOHN | ENGLISH |
2 | ROBERT | COMPUTER SCIENCE |
3 | SASHA | COMMUNICATIONS |
4 | JULIAN | MATHEMATICS |
1 | JOHN | PHYSICAL EDUCATION |
2 | ROBERT | GYM |
3 | SASHA | FILM |
SQL UNION ALL Operator
UNION ALL is an SQL operator used to combine data from multiple tables into a single result set, including all duplicate rows.
For example, consider two tables: one listing the number of international games a player has played, and the other listing national games.

In the tables above, Kohli played 234 matches internationally and 234 matches nationally. Even though the numbers are the same, these represent separate matches. To include both rows in the combined result showing the total matches played, we use the UNION ALL operator.

Syntax
Following is the syntax of UNION ALL operator in SQL:
SELECT * FROM table1 UNION ALL SELECT * FROM table2;
Example
Let us perform a UNION ALL operation on the sample tables COURSES_PICKED and EXTRA_COURSES_PICKED. The query for this operation is shown below:
SELECT * FROM COURSES_PICKED UNION ALL SELECT * FROM EXTRA_COURSES_PICKED;
The resultant table is displayed as follows:
STUDENT_ID | STUDENT_NAME | COURSE_NAME |
---|---|---|
1 | JOHN | ENGLISH |
2 | ROBERT | COMPUTER SCIENCE |
3 | SASHA | COMMUNICATIONS |
4 | JULIAN | MATHEMATICS |
1 | JOHN | PHYSICAL EDUCATION |
2 | ROBERT | GYM |
3 | SASHA | FILM |
4 | JULIAN | MATHEMATICS |