Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • PostgreSQL - Get Started
  • Install PostgreSQL
  • Connect to PostgreSQL DB
  • Create Database
  • Create Table
  • Copy Table
  • Drop Table
  • Drop Database
  • Truncate Table
  • ALTER Table
  • Rename Table
  • Rename Columns
  • Add Columns
  • Modify Column Type
  • Set Default Value of Column
  • Remove Columns
  • Add Constraints to Table
  • Insert Data
  • Upsert Data
  • Update Data
  • Delete Data
  • SELECT Statement
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • DISTINCT Clause
  • Inner Join
  • Left Outer Join
  • Right Outer Join
  • Full Outer Join
  • Self Join
  • Natural Join
  • Cross Join
  • LIMIT OFFSET Clause
  • GROUPING SETS
  • GROUPING() Function
  • GROUP BY CUBE
  • GROUP BY ROLLUP
  • Sub Query
  • ALL Operator
  • ANY Operator
  • UNION Operator
  • INTERSECT Operator
  • EXCEPT Operator
  • IS NULL Operator
  • BETWEEN Operator
  • LIKE Operator
  • CAST Operator
  • CASE Expressions
  • NULLIF()
  • COALESCE()
  • GREATEST(), LEAST()
  • WITH Queries (CTE)
  • Constraints
  • NOT NULL Constraint
  • Unique Constraint
  • Check Constraint
  • Primary Key
  • Foreign Key
  • Sequence
  • Serial Type
  • Identity Columns
  • Generated Columns
  • Data Types
  • Boolean Type
  • Character Type
  • Integer Type
  • Numeric Type
  • Date Type
  • Time Type
  • TimeStamp Type
  • Interval Type
  • Array Type
  • Json Type
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

PostgreSQL: Left Outer Join

The LEFT JOIN is a type of join where it returns all records from the left table and matching records from the right table.

Here the left table is the table that comes to the left side of the "LEFT JOIN" phrase in the query, and the right table refers to a table that comes at the right side or after the "LEFT JOIN" phrase. It returns NULL for all non-matching records from the right table. In some databases, it is called LEFT OUTER JOIN.

Here is a diagram that represents LEFT JOIN.

Syntax
SELECT <table_1.column_name(s)>, <table_2.column_name(s)>
FROM <table_1>
LEFT [OUTER] JOIN <table_2>
ON <table_1.column_name> = <table_2.column_name>;

As per the above syntax, we have table_1 as the left table and table_2 as the right table and they have some matching columns between them.

For the demo purpose, we will use the following Employee and Department tables.

Notice Department table is parent table with dept_id as a primary key. Employee table is child table that has dept_id as foreign key referencing dept_id column of Department table. Now let's join both the first table (Employee) with a second table (Department) by matching values of the dept_id column.

The Employee table has some employee, for e.g. emp_id = 5 who does not have dept id assigned to it and for department with dept_id = 4, there is no employee. Let's use LEFT JOIN to select data from an Employee that may or may not have matching data in the Department table.

Example: Left Outer Join
SELECT emp.emp_id, emp.first_name, emp.last_name, dept.dept_id, dept.dept_name 
FROM Employee emp LEFT JOIN department dept
ON emp.dept_id = dept.dept_id;

Here, the LEFT OUTER JOIN selects all rows of a left table that is the Employee. It may or may not have matching data in the right table, that is Department. For matching data, it will show dept_id and dept_name column values from the Department table and for non-matching rows, it will show NULL.

For emp_id = 5, there is no department defined by keeping dept_id = null, so the above query shows NULL value in dept_id and dept_name columns.

From the resultset, we can select any column from the Employee or Department table in the SELECT clause. As you can see, in the above query we selected emp_id, first_name and last_name from the Employee table and dept_id and dept_name from the Department table.

If Employee and Department both tables have the same column name used in the ON clause, like in our case dept_id, then you can use the USING syntax like this.

Example: Left Outer Join
SELECT emp.emp_id, emp.first_name, emp.last_name, dept.dept_id, dept.dept_name 
FROM Employee emp LEFT JOIN department dept
USING (dept_id);
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.