
- 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
Difference Between JOIN and UNION in SQL
Both JOIN and UNION are used in SQL to combine data from multiple tables, but they differ significantly in how the data is combined and presented.
- JOIN combines columns from two or more tables based on a related column between them.
- UNION combines rows from two or more queries, stacking the results vertically, regardless of whether the tables are directly related.
What is JOIN in SQL?
A JOIN in SQL is used to combine columns from two or more tables based on a common field between them.
- It returns a single result set with columns from multiple tables.
- If there are rows in one table without a corresponding match in another table, the type of join (INNER, LEFT, RIGHT, FULL) determines whether those rows appear in the result.
- JOINs are usually used to create relational views of data where information from multiple tables needs to be displayed together.
There are two main types of JOINs in SQL:
- Inner Join
- Outer Join
An Inner Join is the basic type of join and retrieves only the rows where there is a match in the common columns of both tables. It is the default join type.
An Outer Join returns both matched and unmatched rows from the tables, depending on the type. It has three subtypes:
- Left Join: Returns all rows from the left table, with matching rows from the right table. Unmatched right table rows appear as NULL.
- Right Join: Returns all rows from the right table, with matching rows from the left table. Unmatched left table rows appear as NULL.
- Full Join: Returns all rows when there is a match in either table. Unmatched rows from both tables appear as NULL where there is no match.
Syntax
Following is the basic syntax of a Join operation in SQL:
SELECT table1.column1, table2.column2... FROM table1 JOIN table2 ON table1.column_name = table2.column_name;
Example
Let us first create two table "COURSES_PICKED" and "EXTRA_COURSES_PICKED" with the same number of columns having 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');
Create 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', 'PHOTOGRAPHY');
Now, let us join the tables COURSES_PICKED and EXTRA_COURSES_PICKED, using the JOIN query as follows:
SELECT c.STUDENT_ID, c.STUDENT_NAME, c.COURSE_NAME, e.EXTRA_COURSE_NAME FROM COURSES_PICKED c JOIN EXTRA_COURSES_PICKED e ON c.STUDENT_ID = e.STUDENT_ID;
The resultant table will be displayed as follows:
STUDENT_ID | STUDENT_NAME | COURSE_NAME | EXTRA_COURSE_NAME |
---|---|---|---|
1 | JOHN | ENGLISH | PHYSICAL EDUCATION |
2 | ROBERT | COMPUTER SCIENCE | GYM |
3 | SASHA | COMMUNICATIONS | FILM |
4 | JULIAN | MATHEMATICS | PHOTOGRAPHY |
What is UNION in SQL
A UNION in SQL is used to combine the results of two or more SELECT queries into a single result set.
- It returns all rows from multiple SELECT queries as a single vertical stack.
- Each SELECT query must have the same number of columns, and the corresponding columns must have same data types.
- By default, UNION removes duplicate rows. Using UNION ALL preserves duplicates.
In UNION operation, column names of first table will become column names of resultant table, and contents of second table will be merged into resultant columns of same data type.
Syntax
Following is the syntax of the SQL UNION operator:
SELECT * FROM table1 UNION SELECT * FROM table2;
Example
Let us combine the tables COURSES_PICKED and EXTRA_COURSES_PICKED created above 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:
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 | PHOTOGRAPHY |
Difference Between JOIN and UNION in SQL
JOIN and UNION are both used to combine data from multiple tables in SQL, but they work in very different ways. The table below summarizes their differences:
Feature | JOIN | UNION |
---|---|---|
Definition | Combines columns from two or more tables based on a related column. | Combines rows from two or more SELECT queries into a single result set. |
Orientation | Horizontal (adds columns) | Vertical (adds rows) |
Purpose | Used to retrieve related data from multiple tables in a single query. | Used to merge results from multiple queries that have the same number of columns and compatible data types. |
Types | INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, CROSS JOIN. | UNION and UNION ALL. |
Duplicate Handling | Does not remove duplicate rows unless DISTINCT is explicitly used | Removes duplicate rows by default; UNION ALL keeps duplicates. |
Columns Requirement | Columns can differ between tables; only matching column(s) are used for join condition. | All SELECT queries must have the same number of columns with compatible data types. |
Relation Requirement | Requires a common column (key) between tables for meaningful combination (except CROSS JOIN). | No relation between tables required; only matching column count and compatible data types. |
Result Set | Single combined row per matching key; unmatched rows appear depending on join type. | All rows from both queries are stacked; order can be managed using ORDER BY at the end. |
Use Case | Used when we want to display related data together, like employees with their department details. | Used when we want to combine results from different queries into a single list, like combining employees from multiple departments. |
SQL Syntax Example | SELECT A.id, A.name, B.salary FROM Employees A JOIN Salaries B ON A.id = B.emp_id; | SELECT name, salary FROM Employees_US UNION SELECT name, salary FROM Employees_UK; |
Conclusion
JOIN and UNION in SQL are both used to combine data, but in different ways. JOIN merges columns horizontally from multiple tables based on a relationship, while UNION stacks rows vertically from multiple SELECT queries.
Choosing between them depends on whether you want to combine data side-by-side from related tables (JOIN) or stack multiple query results into a single list (UNION).