DB2 Tutorial

  • Home
  • Start Here
  • Basics
  • Views
  • Triggers
  • Indexes
  • Functions
    • Aggregate Functions
    • Date Functions
    • String Functions
    • Window Functions
Home » Db2 Basics » Db2 UNION

Db2 UNION

Summary: in this tutorial, you will learn how to use the Db2 UNION to combine the result sets of two or more subselects into a single result set.

Db2 UNION Overview

Db2 UNION operator allows you to combine the result sets of two or more subselects into a single result set.

When Db2 encounters a UNION operator, it carries the following operations:

  • First, process each subselect to form an interim result table.
  • Then, combine these interim tables and delete the duplicate rows to form the final result set.

Here is the syntax of the Db2 UNION:

subselect_1 UNION subselect_2 </code>

The queries in the above syntax must meet the following requirements:

  • Both subselects must have The number and the order of the columns.
  • The data types of the corresponding columns must be the same or compatible via implicit conversion.

The UNION operator is useful to marge lists of values retrieved from multiple tables.

Db2 UNION example

We will use the customers and contacts tables created in the join tutorial for the demonstration.

db2 join customers table
db2 join contacts table

The following query uses the UNION operator to combine the customers and contacts into a single list. To distinguish customers and contacts, we add a new column named person_type.

SELECT customer_id person_id, name, 'customer' person_type FROM customers UNION SELECT contact_id person_id, name, 'contact' person_type FROM contacts;

Here is the result set:

Db2 UNION Operator Example

Using UNION with ORDER BY clause

When you use the ORDER BY clause in a query that uses the UNION operator:

1) You place the ORDER BY clause after the last subselect:

SELECT select_list FROM table_1 UNION SELECT select_list FROM table_2 ORDER BY sort_expression;

2) You can use the column name in the ORDER BY clause if the column in the list has a name. In case the column of a result set does not have a name, you can use the column alias to assign it a name or use a positive integer in the ORDER BY clause to order the rows.

SELECT expression AS name FROM table_1 UNION SELECT ... FROM table_2 ORDER BY name;

or

SELECT expression FROM table_1 UNION SELECT ... FROM table_2 ORDER BY 1

DB2 UNION vs. UNION ALL

The UNION removes all duplicate rows by default. However, if you want to retain the duplicates, you use UNION ALL instead:

subselect_1 UNION ALL subselect_2

Db2 UNION ALL example

The following example uses UNION operator that removes all duplicate rows:

SELECT name FROM customers UNION SELECT name FROM contacts ORDER BY name;
Db2 UNION example

The following picture illustrates the union of two result sets: customers and contacts.

Db2 UNION

However, the following example uses the UNION ALL operator that retains the duplicate rows:

SELECT name FROM customers UNION ALL SELECT name FROM contacts ORDER BY name;
Db2 UNION ALL example

The following picture illustrates the union all of two result sets: customers and contacts.

Db2 UNION ALL

UNION vs. JOIN

The join clause combines columns from two or more tables while the UNION operator combines rows from two or more subselects.

In other words, join appends the result sets from tables horizontally while UNION appends result sets from subselects vertically.

In this tutorial, you have learned how to use the Db2 UNION to combine rows from two or more subselects into a single result set.

  • Was this tutorial helpful ?
  • YesNo
Previous Db2 Subquery
Next Db2 INTERSECT

Getting Started

  • What is Db2
  • Installing Db2 Database Server
  • Db2 Sample Database
  • Creating a Db2 Sample Database
  • Connecting to a Db2 Database
  • Interacting with Db2 using SQL Developer

Data Manipulation

  • SELECT
  • ORDER BY
  • WHERE
  • SELECT DISTINCT
  • AND
  • OR
  • BETWEEN
  • LIKE
  • IN
  • LIMIT
  • FETCH
  • Join
  • INNER JOIN
  • LEFT JOIN
  • RIGHT JOIN
  • FULL OUTER JOIN
  • Self-Join
  • CROSS JOIN
  • GROUP BY
  • Subquery
  • HAVING
  • UNION
  • INTERSECT
  • EXCEPT
  • Common Table Expression or CTE
  • INSERT
  • INSERT Multiple Rows
  • INSERT INTO SELECT
  • UPDATE
  • DELETE

Managing Database Objects

  • CREATE TABLE
  • Identity Columns
  • ALTER COLUMN
  • ADD COLUMN
  • DROP COLUMN
  • DROP TABLE
  • TRUNCATE TABLE
  • RENAME TABLE

Db2 Constraints

  • Primary Key
  • Foreign Key
  • DEFAULT
  • NOT NULL
  • CHECK
  • UNIQUE

Db2 Data Types

  • Integer
  • Decimal
  • VARCHAR
  • CHAR
  • DATE
  • TIME
  • TIMESTAMP

Useful Functions & Expressions

  • CAST
  • CASE Expression
  • COALESCE

About db2tutorial.com

The db2tutorial.com website provides you with a comprehensive IBM DB2 tutorial with many practical examples and hands-on sessions.

Recent Tutorials

  • Db2 Functions
  • Db2 RANK
  • Db2 ROW_NUMBER
  • Db2 Window Functions
  • What is Db2

Site Links

  • Home
  • About Us
  • Contact Us
  • Privacy Policy
Copyright © © 2021 by www.db2tutorial.com. All Rights Reserved.