VOOZH about

URL: https://www.geeksforgeeks.org/sql/sql-union-operator/

⇱ SQL UNION Operator - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

SQL UNION Operator

Last Updated : 13 Apr, 2026

The SQL UNION operator is used to combine the result sets of two or more SELECT queries into a single output. It removes duplicate rows and returns only unique records from all combined queries.

  • UNION ALL includes all rows, even duplicates.
  • Queries must have matching columns and data types.
  • Helpful for combining data from different sources.

Example: First, we create a demo SQL database and tables, on which we will use the UNION Operator command.

👁 A
Table1
👁 B-new
Table2

Query:

SELECT city FROM Table1
UNION
SELECT city FROM Table2;

Output:

👁 Berlin

Syntax:

SELECT column_name FROM table1
UNION
SELECT column_name FROM table2;

Examples of SQL UNION

Let's look at an example of UNION operator in SQL to understand it better. Let's create two tables "Emp1" and "Emp2";

👁 EMP-1
Emp1 Table


👁 Emp-2
Emp2 Table

Example: SQL UNION Operator

 In this example, we find the countries (only unique values) from both the "Table1" and the "Table2" tables: 

Query:

SELECT Country FROM Emp1
UNION
SELECT Country FROM Emp2
ORDER BY Country;

Output:

👁 Country
Output after applying Union operator

Note: The UNION operator combines the results of two queries and removes duplicate rows, while UNION ALL includes all rows from both queries without eliminating duplicates.

Comment
Article Tags:
Article Tags: