SQL Server BasicsWhat is SQL Server database?What is RDBMS?What is Normalization?Why we use Denormalization?What_is_SQL?What is PL/SQL?Difference between SQL and PL/SQLDatabase TableOne to One RelationshipOne to Many RelationshipMany to Many RelationshipMany to One RelationshipString Data TypesNumber Data TypesDate Data TypesOther Data TypesCreate DatabaseDrop DatabaseCreating and Managing Users in SQL ServerCreate TableAlter TableDrop TableConstraints in SQL serverPrimary KeyForeign KeyUnique KeyCandidate KeyComposite KeyDifference between primary key and candidate keyPrimary key and foreign key relationshipSurrogate KeyCascading Referential Integrity ConstraintsSelf Referential Integrity ConstraintsInsert into statementInsert multiple recordsUpdate statementDelete statementTruncate statementDifference between Delete and TruncateAlias in SQL ServerSelect statementSelect DistinctSelect TopSelect IntoNull Functions(ISNULL(),NULLIF(),COALESCE())Sub QueryIdentity ColumnSequence objectDifference between sequence and identity columnSQL Server ClausesWHERE ClauseOrder By ClauseTop N ClauseGroup By ClauseHaving ClauseDifference between Where and HavingSQL Server OperatorsArithmetic OperatorsComparison OperatorsLogical OperatorsBitwise OperatorsAny OperatorsAll OperatorsUnion OperatorsUnion All OperatorsDifference between Union and Union AllIntersect OperatorExcept OperatorDifference between Except and IntersectJoinsInner JoinLeft JoinRight JoinFull JoinSelf JoinCross JoinViewsWhat are views?Create views using SSMSIndexed ViewsComplex ViewsCheck Option in ViewCheck Encryption in ViewSchema Binding Option in ViewRead-only ViewsUpdatable ViewsAdvantages and disadvantages of viewsCreate multiple views on one tableCan we implement index on views?Can we Perform Insert, update, delete operation on views?Stored Procedure and FunctionsWhat are Stored Procedures?Why we use stored procedures?Passing parameters to Stored procedureUser-Defined FunctionsDifference between UDF and Stored procedurePre-Defined Functions@@Indentity and Scope_IndentityNULLIF, ISNULL and COALESCE

SQL - Self Join

In SQL Server, a self join is a technique used to join a table with itself. It allows you to combine rows from the same table based on a specified relationship between them. This is useful when you need to query hierarchical data or compare records within the same table.

The basic syntax for performing a self join in SQL Server is as follows:


SELECT column_list
FROM table1 AS t1
JOIN table1 AS t2 ON join_condition;

In this syntax, column_list represents the columns you want to select from the self-joined table. The table is aliased as t1 and t2 to distinguish between the two instances of the table within the join. The join_condition specifies the relationship between the rows in the self-joined table.

The join condition is typically defined using the columns of the table to establish the relationship. For example, if you have a "Employees" table with columns such as "EmployeeID" and "ManagerID," you can join the table with itself using the join condition t1.ManagerID = t2.EmployeeID.

Here's an example to illustrate the usage of a self join:

Consider a table called "Employees" with the following structure:

Employees Table
ID Name Manager
1 John 3
2 Jane 3
3 Mike NULL
4 Susan 1

To retrieve the names of employees along with the names of their managers, you can use a self join as follows:


SELECT e1.Name AS EmployeeName, e2.Name AS ManagerName
FROM Employees e1
JOIN Employees e2 ON e1.Manager = e2.ID;

In the above query, we select the name of the employee from e1 and the name of the manager from e2. We perform a self join on the "Employees" table using the join condition e1.Manager = e2.ID, which establishes the relationship between an employee and their manager based on the ManagerID and EmployeeID columns.

The output of the query would be:

EmployeeName ManagerName
John Mike
Jane Mike
Susan John

As you can see, the result set includes the names of employees along with the names of their respective managers. The self join allows us to establish the hierarchical relationship between the employees and their managers within the same table.

It's important to note that self joins can be more complex and involve multiple levels of hierarchy, depending on the structure of your data and the relationships you want to explore.

In summary, a self join in SQL Server allows you to join a table with itself, enabling you to retrieve information from different rows within the same table based on a specified relationship. It's commonly used for hierarchical data or when comparing records within the same table.