Sling Academy
Home/PostgreSQL/PostgreSQL: How to Combine Data from 2 Tables (4 examples)

PostgreSQL: How to Combine Data from 2 Tables (4 examples)

Last updated: February 24, 2024

Introduction

In the realm of database management, PostgreSQL stands out for its robust features and flexibility. One common task that database administrators and developers often encounter is combining data from two tables. This might be necessary for various reasons, such as generating reports, conducting analyses, or integrating systems. In PostgreSQL, there are several ways to achieve this, and in this tutorial, we’ll explore four practical examples to combine data from two tables: using JOIN, UNION, INTERSECT, and CTE (Common Table Expressions).

Understanding the Data

Before diving into the examples, let’s establish a common dataset that we’ll use throughout this tutorial. Assume we have two tables: Orders and Customers.

Here’s the Orders table:

OrdersOrderIDOrderDateCustomerIDTotalAmount
 12023-01-01101150
 22023-01-02102250

And this is Customers:

CustomersCustomerIDNameEmail
 101Jane Doe[email protected]
 102John Smith[email protected]

Example 1: Using JOIN

The JOIN operator is one of the most common and versatile methods to combine data from two tables. In this first example, let’s demonstrate how to use a simple INNER JOIN to retrieve a list of orders along with the customer information for each order.

SELECT O.OrderID, O.OrderDate, C.Name, C.Email
 FROM Orders O
 INNER JOIN Customers C
 ON O.CustomerID = C.CustomerID;

This query will return only those orders where a matching customer ID is found in both tables. It’s an efficient way to combine relevant information without having extra or unrelated data.

Example 2: Using UNION

If the goal is to combine results from two select queries into a single result set, the UNION operator can be used. It’s important to note that UNION removes duplicate records. Here, we’ll simulate combining two separate queries reflecting orders from two different regions, hypothetical tables Orders_North and Orders_South.

SELECT OrderID, CustomerID, TotalAmount
 FROM Orders_North
 UNION
 SELECT OrderID, CustomerID, TotalAmount
 FROM Orders_South;

This query combines the data from both tables into a single set, ensuring no duplicate records. It’s a great way to compile comprehensive lists from fragmented datasets.

Example 3: Using INTERSECT

The INTERSECT operator is used when you want to find common records between two queries. Suppose you want to find customers who have made orders from both the Orders_North and Orders_South tables.

SELECT CustomerID
 FROM Orders_North
 INTERSECT
 SELECT CustomerID
 FROM Orders_South;

This query will return the IDs of customers who exist in both tables, effectively finding the intersection of the two datasets.

Example 4: Using CTE (Common Table Expressions)

Common Table Expressions (CTEs) offer a powerful and flexible way to write more complex queries. They allow you to create temporary result sets that can be easily referenced within another SQL statement. In this case, we’ll use a CTE to combine data from the Orders and Customers tables, where we aim to create a summary report showing total orders per customer.

WITH CustomerOrders AS (
   SELECT C.CustomerID, C.Name, COUNT(O.OrderID) AS TotalOrders
   FROM Customers C
   LEFT JOIN Orders O
   ON C.CustomerID = O.CustomerID
   GROUP BY C.CustomerID
 )
 SELECT * FROM CustomerOrders;

In this example, the CTE CustomerOrders creates a temporary view of customers along with a count of their total orders. This enables a direct, readable syntax for generating aggregated reports or summaries combining data across tables.

Next Article: Using the STRPOS Function in PostgreSQL

Previous Article: Working with Temporary Tables in PostgreSQL

Series: PostgreSQL Tutorials: From Basic to Advanced

PostgreSQL

You May Also Like

  • PostgreSQL: How to reset the auto-increment value of a column
  • PostgreSQL: How to add a calculated column in SELECT query
  • PostgreSQL: How to Drop FOREIGN KEY Constraints
  • Composite Indexes in PostgreSQL: Explained with Examples
  • Exploring GIN (Generalized Inverted Indexes) in PostgreSQL (with Examples)
  • Custom Collations and Types in PostgreSQL: The Complete Guide
  • Understanding Hash Indexes in PostgreSQL
  • PostgreSQL GiST (Generalized Search Tree) Indexes: Explained with Examples
  • PostgreSQL: Making Use of BRIN (Block Range Indexes)
  • PostgreSQL SP-GiST (Space-Partitioned Generalized Search Tree)
  • Bloom Filters in PostgreSQL: A Practical Guide
  • PostgreSQL: Using Partial Indexes to Improve Efficiency
  • PostgreSQL: Deleting orphan rows in one-to-many relationship
  • PostgreSQL: Implementing fixed-size tables with triggers
  • How to Use Loops in PostgreSQL (with Examples)
  • Working with Temporary Tables in PostgreSQL
  • PostgreSQL: 4 ways to store comments and nested comments
  • PostgreSQL: Saving categories and subcategories in one table
  • PostgreSQL: How to store images in database (and why you shouldn’t)