DEV Community

Hannan2910
Hannan2910

Posted on

Exploring Different Types of Joins in PostgreSQL

Introduction

Understanding the technique of connecting tables is essential for getting useful insights out of databases. PostgreSQL provides a range of join methods for relational databases to effectively merge data from various tables. We'll explore joins in this blog article, learn about their different kinds, and use real-world examples to explain each.

Joins in PostgreSQL

Since joins allow us to merge data from many tables based on shared columns, they form the foundation of relational database systems. PostgreSQL, an innovative open-source RDBMS, offers various forms of joins, each having a distinct function. Let's explore a few of the most typical join types.

Inner Join

The most common join type is probably the inner join, often called an equijoin. Only entries with matching values in the designated columns of both tables are retrieved. Think about an imaginary online store where you want to retrieve orders along with customer data.

SELECT orders.order_id, customers.customer_name
FROM orders
INNER JOIN customers ON orders.customer_id = customers.customer_id;
Enter fullscreen mode Exit fullscreen mode

Left Join

With a left join, all data from the left table is retrieved along with any matches from the right table. The result will have NULL values for the columns of the right table if there is no match in the right table. For situations like fetching products along with their reviews, this kind of join is helpful.

SELECT products.product_name, reviews.review_text
FROM products
LEFT JOIN reviews ON products.product_id = reviews.product_id;
Enter fullscreen mode Exit fullscreen mode

Right Join

A right join, on the other hand, obtains every record from the right table as well as any matches from the left table. The result will include NULL values for the columns of the left table if there is no match in the left table. Although less frequent, this kind of join can be advantageous in some circumstances.

SELECT employees.employee_name, departments.department_name
FROM employees
RIGHT JOIN departments ON employees.department_id = departments.department_id;
Enter fullscreen mode Exit fullscreen mode

Full Outer Join

With a full outer join, all data from both tables is retrieved, and blank spaces are filled with NULL if there is no match. When you want to see a complete picture of the data, including mismatched records from both tables, this join type comes in handy. Consider that you wish to examine both product sales and returns.

SELECT sales.product_name, returns.return_reason
FROM sales
FULL OUTER JOIN returns ON sales.product_id = returns.product_id;
Enter fullscreen mode Exit fullscreen mode

Conclusion

Learning PostgreSQL's many join types gives  a world of opportunities for searching and analysing data across multiple tables. Understanding and using the appropriate join type is an essential ability for any database worker, and whether you need to acquire related information, incorporate unmatched records, or build thorough reports.

Top comments (1)

Collapse
 
linuxguist profile image
Nathan S.R.

Thanks for this very useful post. I just wanted to add that, there is a very easy way now, to test all the SQLs described here, using the free & portable tools, mentioned in my latest post here : dev.to/linuxguist/learn-sql-quickl...