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: Self Join

The self join is a join that joins a table to itself. The self join is mainly used to query hierarchical data or compare rows of a table.

In self join, the same table is used twice with different table aliases. The INNER JOIN, LEFT JOIN or RIGHT JOIN can be used to perform Self join.

Syntax
SELECT <table_1.column_name(s)>, <table_1.column_name(s)>
FROM <table_1>
INNER JOIN <table_1>
ON <table_1.column_name> = <table_1.column_name>;

For the demo purpose, we will use the following Employee table.

In the above Employee table, the manager_id column references to emp_id column. The employee has manager assigned and manager will be an employee. The value in manager_id column shows the manager that the employee directly reports to. When manager_id column is null for any employee that means that the employee does not report to anyone, that employee might be a top manager.

The following query shows hierarchical data of manager and employee.

Example: Self Join
SELECT
	emp.emp_id employee_id,
    emp.first_name || ' ' || emp.last_name employee,
	mgr.emp_id manager_id,
    mgr.first_name || ' ' || mgr.last_name manager
FROM employee emp INNER JOIN employee mgr 
ON emp.manager_id = mgr.emp_id
ORDER BY manager;

The above query references the Employee table twice once as Employee and the other as the Manager and uses INNER JOIN. We have given table alias as emp for Employee and mgr for the manager.

An Employee with emp_id 5 and 7 have manager as 4, employees with emp_id 1 and 3 have manager as 2. Employee with emp_id 2 and 4 reports to employee id 6. Notice that employee id 6 does not have any manager assigned, he is at the top level and not shown as a row in the above query result. To show top-level employee also, use LEFT JOIN.

Example: Self Join
SELECT
	emp.emp_id employee_id,
    emp.first_name || ' ' || emp.last_name employee,
	mgr.emp_id manager_id,
    mgr.first_name || ' ' || mgr.last_name manager
FROM employee emp LEFT JOIN employee mgr 
ON emp.manager_id = mgr.emp_id
ORDER BY manager;

Self join can be used to compare data of the same table. Let's compare data of Employee table with itself and list employees that belong to same department.

Example: Self Join
SELECT
    emp1.first_name || ' ' || emp1.last_name employee,
    emp2.first_name || ' ' || emp2.last_name employee,
	emp1.dept_id department
FROM employee emp1 INNER JOIN employee emp2
ON emp1.emp_id <> emp2.emp_id
AND emp1.dept_id = emp2.dept_id;

In the above query, we took the INNER JOIN of employee table with itself. We used emp1.emp_id <> emp2.emp_id to join tables and emp1.dept_id = emp2.dept_id to compare the dept_id of employees.

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.