Inner Joins between Subsets of the Same Table.

  • Thread starter Thread starter WWGD
  • Start date Start date
  • Tags Tags
    Subsets Table
Click For Summary

Discussion Overview

The discussion revolves around SQL syntax for performing inner joins on subsets of the same table, specifically in the context of employee management and hierarchical relationships. Participants explore different methods for querying employee data, including self-joins and the use of standard versus proprietary SQL syntax.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant seeks clarification on the syntax for joining subsets of the same table to find out who each employee reports to, presenting an initial query attempt.
  • Another participant suggests a simpler approach using a self-join and provides an alternative query that achieves the same result without complex syntax.
  • A third participant mentions that using a left join would include employees who do not report to anyone, expanding the query's results.
  • One participant references a resource for self-joins, indicating a potential solution for the original poster's query.
  • Another participant discusses the use of SQL's 'start with ... connect by' syntax for building hierarchical data structures from a single table, noting its non-standard status in SQL.
  • A later post raises a question about querying data from three different tables and proposes a method for joining these tables while considering grouping by customer name.
  • Another participant reiterates the 'start with ... connect by' syntax while emphasizing the existence of standard syntax for recursive queries supported by a broader range of SQL vendors.

Areas of Agreement / Disagreement

Participants express differing opinions on the best approach to writing SQL queries for self-joins and hierarchical data. While some suggest simpler methods, others advocate for more complex syntax, indicating that no consensus exists on a single best practice.

Contextual Notes

There are mentions of proprietary SQL syntax versus standard SQL syntax, highlighting limitations in compatibility across different database systems. The discussion also reflects varying levels of familiarity with SQL concepts among participants.

Who May Find This Useful

Readers interested in SQL query construction, particularly those dealing with hierarchical data or self-joins, may find the discussion beneficial.

WWGD
Science Advisor
Homework Helper
Messages
7,802
Reaction score
13,106
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   Reactions: 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   Reactions: 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   Reactions: jim mcnamara and WWGD

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
1
Views
18K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 96 ·
4
Replies
96
Views
12K
  • · Replies 2 ·
Replies
2
Views
2K