[WEBINAR] Application Acceleration: Faster Performance for End Users


Definition - What does Self-Join mean?

A self-join, also known as an inner join, is a structured query language (SQL) statement where a queried table is joined to itself. The self-join statement is necessary when two sets of data, within the same table, are compared.

Techopedia explains Self-Join

As an example, there is a table named EMPLOYEES that contains three columns:

  • employee name
  • employee ID
  • employee manager’s ID

Because the managers are also employees, the MANAGER_ID column also contains the ID of another employee that is also the manager. To write a query to extract the employee and manager names and IDs, the table must be logically split in half to run two separate queries: employees (first table) and managers (second table). This is achieved by running the following sample SQL query:

SELECT a.employee_name, b.employee_name as Manager_name
FROM employees as a, employees as b
WHERE a.manager_id = b.employee_id

Understanding the self-join concept and circumstances is essential to grasping the above SQL statement.

In the example, the second EMPLOYEES table is given the alias b, which actually is a subset of the full EMPLOYEES table. However, the WHERE condition forces the first EMPLOYEES table to query the employee manager in the second EMPLOYEES table.

Techopedia Deals

Connect with us

Techopedia on Linkedin
Techopedia on Linkedin
"Techopedia" on Twitter

Sign up for Techopedia's Free Newsletter!

Email Newsletter

Join thousands of others with our weekly newsletter

Free Whitepaper: The Path to Hybrid Cloud
Free Whitepaper: The Path to Hybrid Cloud:
The Path to Hybrid Cloud: Intelligent Bursting To Amazon Web Services & Microsoft Azure
Free E-Book: Public Cloud Guide
Free E-Book: Public Cloud Guide:
This white paper is for leaders of Operations, Engineering, or Infrastructure teams who are creating or executing an IT roadmap.
Free Tool: Virtual Health Monitor
Free Tool: Virtual Health Monitor:
Virtual Health Monitor is a free virtualization monitoring and reporting tool for VMware, Hyper-V, RHEV, and XenServer environments.
Free 30 Day Trial – Turbonomic
Free 30 Day Trial – Turbonomic:
Turbonomic delivers an autonomic platform where virtual and cloud environments self-manage in real-time to assure application performance.