SQL join when tables are separated by one or more other tables

Say in an SQL database I have an "Employee" table, which points to a "Person" table, which points to a Person_Name table:

Employee (table)
+------------+-----+      Person (table)
| Field      | Key |      +------------+-----+      Person_Name (table)
+------------+-----+      | Field      | Key |      +------------+-----+
| employee_id| PK  |      +------------+-----+      | Field      | Key |
| person_id  | FK  | -->  | person_id  | PK  |      +------------+-----+
+------------+-----+      | name_id    | FK  | -->  | name_id    | PK  |
                          +------------+-----+      | people_id  | FK  |
                                                    | name       |     |
                                                    +------------+-----+

Open in new window

What query would list me all employees and their names?
deleydAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Give this a whirl...
SELECT e.employee_id, pn.name
FROM Employee e 
   JOIN Person p ON e.person_id = p.person_id
   JOIN Person_Name pn ON p.name_id = pn.name_id
ORDER BY pn.name

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
deleydAuthor Commented:
Thank you for the query. I seem to get no result from it. I created a test database and filled it in as follows:
Employee (table)              Person (table)               Person_Name (table)
+------------+---------+      +------------+--------+      +----------+------+
| employee_id|person_id|      | person_id  | name_id|      | name_id  | name |
+------------+---------+      +------------+--------+      +----------+------+
|     1      |   10    |      |    10      |  100   |      |   100    | One  |
|     2      |   20    | -->  |    20      |  200   | -->  |   200    | Two  |
|     3      |   30    |      |    30      |  300   |      |   300    | Three|
+------------+---------+      +------------+--------+      +----------+------+

Open in new window

Then run the query, which doesn't complain, but also gives me no results. Hmm..,
٩(͡๏̯͡๏)۶
(I also removed the people_id from the Person_Name table which served no purpose.)
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
There's strange. Jim's solution should work. Here's something a little bit different only for a better read but shouldn't be because of this that isn't working:
SELECT e.employee_id, pn.name
FROM Employee e 
   INNER JOIN Person p 
        INNER JOIN Person_Name pn ON p.name_id = pn.name_id
    ON e.person_id = p.person_id
ORDER BY pn.name

Open in new window


I'm assuming these tables are only for sampling and isn't your real situation or I would say that you could get rid of the Person_Name table and add the name column directly in the Person table.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

deleydAuthor Commented:
Thank you for your help. I'm stumped on this one, why I'm getting no results, so I made some screen shots and attached them here.  Hopefully  someone can spot the problem.
Person Database
Person DatabaseName Database
Name DatabaseEmployee Database
Employee DatabaseQuery2 Results
Query2 ResultsQuery1 Results
Query1 Results
0
deleydAuthor Commented:
Oh I see the problem.
0
deleydAuthor Commented:
OK Thank you I'll use the Image button next time.

Both queries work now.

So I see one pattern is:
SELECT
FROM
JOIN
JOIN
ON
ON

and the other pattern is:
SELECT
FROM
JOIN ... ON ...
JOIN ... ON ...

I suppose both work fine. Thank you for your help!
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
So, name_id wasn't equal :)
Machine is always right ;)
0
deleydAuthor Commented:
(I have a T-shirt with a human working at the computer. Human thinks, "Stupid computer." Computer thinks, "Stupid human.")

:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.