Inner Joins between Subsets of the Same Table.

In summary, the conversation is about trying to figure out the syntax for doing joins between subsets of the same table. The suggested solution is to use a self join and the "start with ... connect by" syntax or the standard syntax for recursive queries. The final query should select the OrderNum, CustomerName, and QuotedPrice from the Orders, Customer, and OrderLine tables, by joining Orders with Customer and then joining the result to OrderLine.
  • #1
WWGD
Science Advisor
Gold Member
7,123
10,778
Hi, All, I am trying to figure out the syntax for doing joins between subsets of the same table.

I have:
Employee ( EmpId PK , EmpFirst, EmpLast, EmpMid, DateHired, SSN, DateBirth, Gender, PhoneNum, ReportsTo)

And I want to find , for each employee, the person they report to.
So I am thinking of using:
-------------------------------------------------------
select empFirst, emplast, empId as Managers inner join (select employeeid, empfirstname, emplastname, reportsTo, from Employee) AS Staff

on

Managers.employeeid= Staff.reportsTo.
-------------------------------------------------------------

But it seems I need to do something that does not make sense to me, the part between the ** s:
_____________________________________________________________
**select managers.employeeid, managers.empfirstname , managers.emplastname , staffmembers.emplastname , staffmembers.reportsTo **

from (select employeeid, empfirstname, emplastname from employee) AS managers
inner join
(select employeeid , empfirstname, emplastname, reports to from employee) AS Staffon managers.employeeid = staff.reportsTo_____________________________________________________________________
Why do we use the part between the ** s?

Thanks.
 
Technology news on Phys.org
  • #2
Seems complicated.

This should work:

select e.empFirst,e.empLast, m.empFirst as managerFirst,m.empLast as managerLast
from employee as e join employee as m on m.empId=e.reportsTo

In real life I wouldn't even use the Join syntax, but use:

select e.empFirst,e.empLast, m.empFirst as managerFirst,m.empLast as managerLast
from employee as e, employee as m where m.empId=e.reportsTo

These two queries give the same result. The result won't include the top people who report to no one.
If you want these too in the query, use a left join:

select e.empFirst,e.empLast, m.empFirst as managerFirst,m.empLast as managerLast
from employee as e left join employee as m on m.empId=e.reportsTo
 
  • Like
Likes WWGD
  • #4
Samy's example is the best choice given your exmple. Mor generally, for problems like this, there is the SQL 'start with ... connect by' syntax - it can build a tree from a single table.

http://philip.greenspun.com/sql/trees
 
  • Like
Likes WWGD
  • #5
Thanks, both. Is there a standard format for queries requiring fields from 3 or more tables? I need to find OrderNum, CustomerName, Quoted price, which are on tables Orders, Customer and OrderLine respectively.
Orders and Customer are related to each other, so are Order and OrderLine. Should I just first Join Orders with Customer and then join the result to OrderLine (together with the grouping by CustomerName)?

Specifically, I was thinking of doing this:

Select OrderNum, CustomerName, QuotedPrice from
Orders inner join Customer on Orders.CustomerNum = Customer. CustomerNum
inner join Orderline on Orders. OrderNum = Orderline.OrderNum
group by CustomerNum.
 
  • #6
jim mcnamara said:
Samy's example is the best choice given your exmple. Mor generally, for problems like this, there is the SQL 'start with ... connect by' syntax - it can build a tree from a single table.

http://philip.greenspun.com/sql/trees
Worth noting that CONNECT BY is is not standard SQL (either official or industry de-facto). It is supported by a couple of the big players, but not by the other ones. There is standard syntax for such operations, which is supported by a larger set of vendors:

Here is an example (from one vendor, but using standard syntax for recursive queries):

https://msdn.microsoft.com/en-us/library/ms186243.aspx

[Note, for example, that Oracle supports both CONNECT BY, which they originated as proprietary syntax [I believe before any standard syntax existed], as well as supporting the standard syntax described above.]
 
  • Like
Likes jim mcnamara and WWGD

Related to Inner Joins between Subsets of the Same Table.

1. What is an Inner Join?

An Inner Join is a type of SQL query used to combine data from two or more tables based on a common column or key. It only returns rows where the values in the common column match in both tables.

2. How do Inner Joins work?

Inner Joins work by comparing the values in the common column of two or more tables and only returning rows where the values match. This allows for the combination of data from multiple tables into a single result set.

3. Can Inner Joins be performed between subsets of the same table?

Yes, Inner Joins can be performed between subsets of the same table. This allows for the comparison and combination of data within a single table based on a common column or key.

4. What is the difference between Inner Joins and Outer Joins?

The main difference between Inner Joins and Outer Joins is that Inner Joins only return rows where the values in the common column match in both tables, while Outer Joins can also return rows where there is no match in the common column.

5. When should Inner Joins between subsets of the same table be used?

Inner Joins between subsets of the same table can be useful when you need to compare and combine data within a single table based on a common column or key. This can be helpful for data analysis and organization.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Replies
6
Views
1K
  • Advanced Physics Homework Help
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Sci-Fi Writing and World Building
3
Replies
96
Views
6K
  • Introductory Physics Homework Help
Replies
2
Views
3K
  • Art, Music, History, and Linguistics
Replies
1
Views
1K
  • Calculus and Beyond Homework Help
Replies
8
Views
2K
  • Calculus and Beyond Homework Help
Replies
2
Views
1K
Back
Top