Questions tagged [self-join]

A table can be joined to itself and this is called a self-join. You can use a self-join to create a result set that joins records in a table with other records in the same table for example to filter a specific subset of records from within a single table without returning duplicate or similar rows.

Self-joins are useful in queries or subqueries such as the following:

  • a limited result set of rows is needed
  • a relationship between rows is needed
  • a calculation among rows is needed

Self-joins are also useful when converting vendor specific syntax to vendor agnostic syntax

References

1264 questions
-4
votes
2 answers

Select distinct pairs in SQL

For this table each column has distinct rows i.e count(Name) = count(Spouse_Name) = count(*) Want to write a SQL query where pairs (Name and Spouse_Name) are distinct i.e Alex Sandra and Sandra Alex are same. The output should be following: Note:…
afghani
  • 467
  • 5
  • 7
-4
votes
1 answer

Conditional JOIN vs Self JOIN

I am trying to join two tables on two different columns and I was wondering if following two techniques are equivalent, if yes which one is better performance wise? JOIN with OR (Conditional JOIN) SELECT * FROM table1 JOIN TABLE2 ON table1.value =…
Aura
  • 1,283
  • 2
  • 16
  • 30
-7
votes
2 answers

query to find number of employees under each manager

I have an Employee table with columns : Id, Name, Manager_Name I need to list: count of employees under each manager. How to form a simple sql query for achieving result? Note: There may be two managers with same name. Table:- create table…
Deepak S
  • 1,544
  • 3
  • 15
  • 33
1 2 3
84
85