EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial LEFT OUTER JOIN in PostgreSQL
Secondary Sidebar
PostgreSQL Tutorial
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • Postgres Command-Line
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL BIGINT
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
    • Postgres like query
    • PostgreSQL encode
    • PostgreSQL Cheat Sheet
    • PostgreSQL List Databases
    • PostgreSQL Rename Database
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL INSERT INTO
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL ORDER BY DESC
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL Drop Schema
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL User Password
    • PostgreSQL log_statement
    • PostgreSQL repository
    • PostgreSQL shared_buffer
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL where in array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL CHECK Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • PostgreSQL List Tables
    • PostgreSQL TRUNCATE TABLE
    • PostgreSQL Table Partitioning
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL datediff
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL LENGTH()
    • PostgreSQL blob
    • PostgreSQL Median
    • PostgreSQL kill query
    • PostgreSQL Formatter
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL UUID
    • PostgreSQL Merge
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Show Databases
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL Show Tables
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL Encryption
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL Incremental Backup
    • PostgreSQL JSON vs JSONNB
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions

LEFT OUTER JOIN in PostgreSQL

By Priya PedamkarPriya Pedamkar

left outer join in postgresql

Introduction to LEFT OUTER JOIN in PostgreSQL

Left outer join is also known as Left join, it will retrieve all rows from the left table, and matching rows from the right table also retrieve all matching records from both the tables, if we want to fetch all record from the first table and doesn’t need any record from the second table then we create left join the second table such as that column has null values, left join or left outer join is a most important type of join in PostgreSQL, PostgreSQL left join comes in a PostgreSQL outer also join it is types of PostgreSQL outer join.

Syntax

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

The left join will retrieve all rows from the left table and matching records from the right table.

Below is the syntax of the left outer join is as follows:

Syntax #1

Select columns from table_name1 LEFT OUTER JOIN table_name2 on table_name1.column = table_name2.column;

Syntax #2

SELECT (*) FROM table_name1 LEFT OUTER JOIN table_name2 on table_name1.column = table_name2.column;

Working of LEFT OUTER JOIN in PostgreSQL

Following diagram shows the virtual representation of LEFT OUTER JOIN in PostgreSQL:

left outer join in posterql

Explanation of the above diagram:

  • While joining the table using the left outer join, PostgreSQL first does normal join and then it starts scanning from the left table.
  • PostgreSQL left join retrieves all rows from the left table and all matching rows from the right table. If there is no match in both tables, the right tables have null values.
  • The above image representation of left join shows the same as retrieving all rows from table1 and matching rows from table2.
  • PostgreSQL left joins two tables and fetches data row based using condition, which matching from both the tables also fetches unmatched rows from the tables before joining clause.
  • I suppose the ABC table has a left join with a PQR table with join condition (We consider ABC as the left table and PQR as the right table).
  • PostgreSQL left to join the ABC table depends on the PQR table. Also, all tables depend on ABC, and ABC depends on all tables that are being used left join condition except table PQR.
  • Left join condition will decide how to retrieve rows from the PQR table.

In this case, This in PostgreSQL will show below outputs values from ABC and PQR table:

  1. It will take all the selected values from the ABC table.
  2. The value will be combined with the column name from the PQR table.
  3. Retrieves all matching rows from the associated (right table) table.
  4. Then sets the value for every column from the right table (PQR table) to null, which is unmatched from the ABC table.
  • PostgreSQL left join also known as left outer join. It will fetch all the records from the left table and matching records from the right table. If the right table has null values, then no matching record will take place.
  • PostgreSQL left join used to select data from multiple tables in a single query.
  • Retrieving data from multiple tables using PostgreSQL left join, we need to have primary key constraints on the first table and foreign key constraints on the second table.
  • PostgreSQL left outer join is the same as left join in SQL language.
  • PostgreSQL left join comes in types of PostgreSQL outer join.

PostgreSQL outer join has three types:

  1. Left outer join or left join
  2.  Right outer join or right join
  3. Full outer join.
  • In left outer join or left join in PostgreSQL will return all records from table1 and only matching or intersect records from table2.
  • PostgreSQL left join or left outer join is used to retrieve all data from one table and matching record from the second table.

Examples to Implement Left Outer Join

Following is the example of a left outer join are as follows. We have using employee and department tables to describe left join in PostgreSQL.

Example #1

Create an Employee and Department table and insert data into it

Code:

CREATE TABLE employee ( emp_id INT NOT NULL, emp_name character(10) NOT NULL, emp_email character(20) NOT NULL, emp_phone character(14), PRIMARY KEY (emp_id));

Output:

create table

Code:

INSERT INTO EMPLOYEE (emp_id, emp_name, emp_email, emp_phone) VALUES (1, 'ABC', '[email protected]', '1234567890');
INSERT INTO EMPLOYEE (emp_id, emp_name, emp_email, emp_phone) VALUES (2, 'PQR', '[email protected]', '1234567890');
INSERT INTO EMPLOYEE (emp_id, emp_name, emp_email, emp_phone) VALUES (3, 'XYZ', '[email protected]', '1234567890');
INSERT INTO EMPLOYEE (emp_id, emp_name, emp_email, emp_phone) VALUES (4, 'BBS', '[email protected]', '1234567890');
INSERT INTO EMPLOYEE (emp_id, emp_name, emp_email, emp_phone) VALUES (5, 'RBS', '[email protected]', '1234567890');

Output:

left outer join in postgreSQL - 2

Code:

CREATE TABLE department ( dept_name character(10) NOT NULL, dept_id int NOT NULL, dept_code varchar(10));

Output:

create table

Code:

INSERT INTO department (dept_name, dept_id, dept_code) VALUES ('IT', 101, 'IT101');
INSERT INTO department (dept_name, dept_id, dept_code) VALUES ('IT', 101, 'IT101');
INSERT INTO department (dept_name, dept_id, dept_code) VALUES ('HR', 102, 'HR102');
INSERT INTO department (dept_name, dept_id, dept_code) VALUES ('HR', 102, 'HR102');
INSERT INTO department (dept_name, dept_id, dept_code) VALUES ('Sales', 103, 'HR103');

Output:

left outer join in postgreSQL - 4

Code:

select * from department;

Output:

insert

Example #2

Create left join between employee and department table

Code:

SELECT emp_id, employee.emp_name, employee.emp_phone FROM employee LEFT OUTER JOIN department ON employee.emp_id = department.dept_id;

Output:

select employee

Code:

SELECT emp_id, employee.emp_name, department.dept_id, department.dept_name FROM employee LEFT OUTER JOIN department ON employee.emp_id = department.dept_id;

Output:

left outer join in postgreSQL - 6

Explanation of the above tables:

  • In the above table, we have to join the employee and department table. In the first example, all records from the employee table will display. In the second example, there are no matching records in the employee and department table, displaying an empty set.
  • The above example retrieves different outputs from the same table using the same.
  • In our example, the employee table has left the table, and the department table is the right table.
  • We are joining the table using the employee table as the id column and department tables as the dept_id column.
  • The second example of a left join is useful when selecting data from one table and doesn’t have a match from the second table.

Conclusion

A left join is the most important type of join in PostgreSQL. The left join will fetch all rows from the left tables and matching rows from the right table. We need the primary key on the first table for retrieving the data for joining two tables using PostgreSQL left join or left outer join.

Recommended Articles

This is a guide to LEFT OUTER JOIN in PostgreSQL. Here we discuss the basic concept, Syntax, working of left outer join with the help of diagram and respective examples to implement left outer join. You can also go through our other related articles to learn more –

  1. IF ELSE Statement in SQL
  2. Top 4 Types of SQL Outer Join
  3. How to Works PostgreSQL INTERSECT?
  4. OUTER Join in Oracle
  5. SQL Left Join | Examples
Popular Course in this category
SQL Training Program (10 Courses, 8+ Projects)
  10 Online Courses |  8 Hands-on Projects |  80+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more