A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/postgresql/postgresql-union-operator/ below:

PostgreSQL - UNION operator - GeeksforGeeks

PostgreSQL - UNION operator

Last Updated : 12 Jul, 2025

The PostgreSQL UNION operator is a powerful tool used to combine result sets from multiple queries into a single result set. It helps in consolidating data from different sources, making it easier to analyze and report.

From this article, we can better understand the UNION Operator in PostgreSQL

Syntax
SELECT
column_1,
column_2
FROM
table_name_1
UNION
SELECT
column_1,
column_2
FROM
table_name_2;
Rules for Using UNION

The below rules need to be followed while using a UNION operator:

Note: The UNION operator removes all duplicate rows from the query set.

Setting Up Sample Tables

Let's look into some examples of the UNION operator by setting up two sample tables in a sample database(say, sales2020). Let's say table 'sales2020q1' represents the sales of a particular product in the first quarter of 2020 and 'sales2020q2' represents the sales in the second quarter of the same year.

Now let's set up the database following the below procedures. Create the 'sales2020' database using the below command:

PostgreSQL
CREATE DATABASE sales2020;
CREATE TABLE sales2020q1(
    id SERIAL,
    name VARCHAR(50) NOT NULL,
    amount VARCHAR(50),
    PRIMARY KEY (id)
);
CREATE TABLE sales2020q2(
    id SERIAL,
    name VARCHAR(50) NOT NULL,
    amount VARCHAR(50),
    PRIMARY KEY (id)
);
INSERT INTO sales2020q1(name, amount)
VALUES
    ('Raju Kumar', '121232'),
    ('Nikhil Aggarwal', '254789'),
    ('Abhishek Kelenia', '365487');

INSERT INTO sales2020q2(name, amount)
VALUES
    ('Raju Kumar', '457264'),
    ('Nikhil Aggarwal', '365241'),
    ('Abhishek Kelenia', '759864');
PostgreSQL UNION operator Examples

Now that our sample database is ready. Let's implement the UNION operator in a few examples.

Example 1: Combining Data from Two Tables

Here we will use the UNION operator to combine data from both 'sales2020q1' and 'salese2020q2' tables.

Query:

SELECT * FROM
sales2020q1
UNION
SELECT * FROM
sales2020q2;

Output:

Explanation: This query combines the rows from both tables into a single result set, removing duplicate rows.

Example 2: Sorting the Combined Results

Here we will sort the combined result returned by the UNION operator in defending order of "id" by using the ORDER BY clause after combining the data from both 'sales2020q1' and 'salese2020q2' tables.

SELECT * FROM
sales2020q1
UNION ALL
SELECT * FROM
sales2020q2
ORDER BY
name ASC,
amount DESC;

Output:

Explanation: This query combines the rows from both tables without removing duplicates (UNION ALL), and sorts the results as specified.

Important Points About PostgreSQL UNION Operator


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4