PostgreSQL Tutorial

  • Home
  • Administration
  • Views
  • Triggers
  • Stored Procedures
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
Home / PostgreSQL Tutorial / PostgreSQL UNION

PostgreSQL UNION

Summary: in this tutorial, you will learn how to use PostgreSQL UNION operator to combine result sets of multiple queries into a single result.

Introduction to PostgreSQL UNION operator

The UNION operator combines result sets of two or more SELECT statements into a single result set. The following illustrates the syntax of the UNION operator that combines result sets from two queries:

1
2
3
4
5
6
7
8
9
10
11
SELECT
column_1,
column_2
FROM
tbl_name_1
UNION
SELECT
column_1,
column_2
FROM
tbl_name_2;

The following are rules applied to the queries:

  • Both queries must return the same number of columns.
  • The corresponding columns in the queries must have compatible data types.

The UNION operator removes all duplicate rows unless the UNION ALL is used.

The UNION operator may place the rows in the first query before, after or between the rows in the result set of the second query. To sort the rows in the combined result set by a specified column, you use the ORDER BY clause.

We often use the UNION operator to combine data from similar tables that are not perfectly normalized. Those tables are often found in the reporting or data warehouse system.

PostgreSQL UNION examples

Let’s take a look at the following tables:

  • sales2007q1: stores sales data in Q1 2007.
  • sales2007q2: stores sales data in Q2 2007.

sales2007q1 data:

Sales Q1 2007

sales2007q2 data:

Sales Q2 2007

PostgreSQL UNION example

We use the UNION operator to combine data from both tables as follows:

1
2
3
4
5
6
7
SELECT *
FROM
sales2007q1
UNION
SELECT *
FROM
sales2007q2;

The query returns the following result:

PostgreSQL UNION result

PostgreSQL UNION ALL example

There are five rows in the combined result set because the UNION operator removes one duplicate row. To get all rows that include duplicate, you use the UNION ALL operator as follows:

1
2
3
4
5
6
7
SELECT *
FROM
sales2007q1
UNION ALL
SELECT *
FROM
sales2007q2;

PostgreSQL UNION ALL result

PostgreSQL UNION with ORDER BY example

To sort the combined result returned by the UNION operator, you use the ORDER BY clause. You need to put the ORDER BY clause at the last query as the following statement:

1
2
3
4
5
6
7
8
9
10
SELECT *
FROM
sales2007q1
UNION ALL
SELECT *
FROM
sales2007q2
ORDER BY
name ASC,
amount DESC;

PostgreSQL UNION with ORDER BY

If you put the ORDER BY clause at the end of each query, the combined result set will not be sorted as you expected. Because when UNION operator combines the sorted result sets from each query, it does not guarantee the order of rows in the final result set.

In this tutorial, we have shown you how to use the PostgreSQL UNION and UNION ALL to combine the result sets from multiple queries into a single result set.

Related Tutorials

  • PostgreSQL EXCEPT Operator
  • PostgreSQL INTERSECT Operator
Previous Tutorial: PostgreSQL HAVING
Next Tutorial: PostgreSQL INTERSECT Operator

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Fundamentals

  • PostgreSQL Select
  • PostgreSQL Order By
  • PostgreSQL Select Distinct
  • PostgreSQL Where
  • PostgreSQL LIMIT
  • PostgreSQL IN
  • PostgreSQL Between
  • PostgreSQL Like
  • PostgreSQL Inner Join
  • PostgreSQL Left Join
  • PostgreSQL Full Outer Join
  • PostgreSQL Cross Join
  • PostgreSQL Natural Join
  • PostgreSQL Group By
  • PostgreSQL Having
  • PostgreSQL Union
  • PostgreSQL Intersect
  • PostgreSQL Except
  • PostgreSQL Subquery
  • PostgreSQL Insert
  • PostgreSQL Update
  • PostgreSQL Delete
  • PostgreSQL Data Types
  • PostgreSQL Create Table
  • PostgreSQL Alter Table
  • PostgreSQL Drop Table
  • PostgreSQL Truncate Table
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
  • PostgreSQL Foreign Key
  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • PostgreSQL Recursive View
  • Learn PostgreSQL Recursive Query By Example
  • Creating Updatable Views Using the WITH CHECK OPTION Clause
  • PostgreSQL Upsert Using INSERT ON CONFLICT statement
  • How to Generate a Random Number in A Range
  • Using PostgreSQL ADD COLUMN to Add One or More Columns To a Table
  • PostgreSQL Character Types: CHAR, VARCHAR, and TEXT
  • Using PostgreSQL SERIAL To Create Auto-increment Column
  • PostgreSQL Boolean Data Type with Practical Examples
  • Understanding PostgreSQL Timestamp Data Types

More Tutorials

  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Functions
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2016 by PostgreSQL Tutorial Website. All Rights Reserved.