What is a self-join, and how does it work in SQL Server
Loading
What is a self-join, and how does it work in SQL Server
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Tuhin PaulRank #7 · 71.9K reputationPosted Jan 23, 2025, 12:01 AM EST
Part - 1:
A self-join is a type of join where a table is joined with itself, creating multiple aliases of the same table to compare rows within the table.
Key Characteristics
Now lets check this for a CRM solution:
- Organizational Hierarchy
- Map employee-manager relationships
- Track reporting structures
- Analyze team compositions
- Performance Comparisons
- Compare employee salaries
- Identify salary disparities
- Analyze departmental structures
Query Types Demonstrated- Manager Identification
- Links employees to their managers
- Uses LEFT JOIN to include all employees
- Salary Comparison
- Compares employee salaries with managers
- Identifies potential compensation issues
- Hierarchical Reporting
- Generates comprehensive organizational view
- Sorted by department and salary
Performance ConsiderationsTuhin PaulRank #7 · 71.9K reputationPosted Jan 23, 2025, 1:00 AM EST
Part - 2:
-- Self-Join in CRM: Employee Reporting Hierarchy CREATE TABLE Employees ( EmployeeID INT PRIMARY KEY, Name VARCHAR(100), ManagerID INT, Department VARCHAR(50), Salary DECIMAL(10,2) ); -- Insert Sample Data INSERT INTO Employees (EmployeeID, Name, ManagerID, Department, Salary) VALUES (1, 'John Smith', NULL, 'Executive', 150000), (2, 'Alice Johnson', 1, 'Sales', 85000), (3, 'Bob Williams', 1, 'Marketing', 80000), (4, 'Charlie Brown', 2, 'Sales', 65000), (5, 'David Miller', 2, 'Sales', 62000), (6, 'Eve Davis', 3, 'Marketing', 58000); -- Self-Join Queries -- 1. Find Employees with Their Managers SELECT e.EmployeeID, e.Name AS EmployeeName, m.Name AS ManagerName FROM Employees e LEFT JOIN Employees m ON e.ManagerID = m.EmployeeID; -- 2. Identify Employees Earning Less Than Their Managers SELECT e.Name AS EmployeeName, e.Salary AS EmployeeSalary, m.Name AS ManagerName, m.Salary AS ManagerSalary FROM Employees e INNER JOIN Employees m ON e.ManagerID = m.EmployeeID WHERE e.Salary < m.Salary; -- 3. Department-wise Hierarchy with Salary Comparison SELECT e.Department, e.Name AS EmployeeName, e.Salary AS EmployeeSalary, m.Name AS ManagerName, m.Salary AS ManagerSalary FROM Employees e LEFT JOIN Employees m ON e.ManagerID = m.EmployeeID ORDER BY e.Department, e.Salary;Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Jan 24, 2025, 12:07 AM EST
Hi Kiran,
A self-join in SQL Server is when a table is joined with itself. It is used to compare rows within the same table. For example, imagine an Employees table with columns EmployeeID, Name, and ManagerID. Each employee might have a manager who is also listed in the same table. A self-join can help find the name of each employee's manager by joining the table to itself. SQL Server works by treating the same table as if it were two separate tables using table aliases. Here is an example:
SELECT E1.Name AS Employee, E2.Name AS Manager FROM Employees E1 JOIN Employees E2 ON E1.ManagerID = E2.EmployeeID;Technically, it works like SQL Server, which creates a cartesian product of the rows in E1 and E2 but only retains rows where E1 is used.ManagerID = E2.EmployeeID. For optimization, it uses indexes on ManagerID or EmployeeID, if available, to avoid scanning every row.
Thank you!