Inner Joins between Subsets of the Same Table.

  • Thread starter Thread starter WWGD
  • Start date Start date
  • Tags Tags
    Subsets Table
AI Thread Summary
The discussion focuses on SQL syntax for joining subsets of the same table, specifically to identify employees and their managers. The initial query attempts to create a self-join to retrieve employee names alongside their managers, but confusion arises regarding the syntax and structure. A simpler and effective solution is presented using a straightforward join, which retrieves employee and manager names directly. It is noted that a left join can be utilized to include employees without managers. Additionally, the conversation shifts to querying multiple tables, where a user seeks to join the Orders, Customer, and OrderLine tables to extract relevant data. A proposed query structure is discussed, emphasizing the importance of proper joins and grouping. The topic of SQL syntax for hierarchical data is also mentioned, with references to both proprietary and standard methods for recursive queries, highlighting that not all SQL features are universally supported across different database systems.
WWGD
Science Advisor
Homework Helper
Messages
7,678
Reaction score
12,349
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
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
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
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.
 
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
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
8
Views
2K
Replies
10
Views
2K
Replies
1
Views
2K
2
Replies
96
Views
9K
Replies
2
Views
2K
Back
Top