okay, let’s try to figure out what the query is doing
first of all, the fact that employees is the 4th table in a series of LEFT OUTER JOINs, along with a specific condition on employees.companyId=67 in the WHERE clause, means that you are in effect getting the results of inner joins
change the query to use INNER JOINs and it returns the same results
okay, now let’s build up the query as we go, join by join
here’s the first step –
SELECT users.userId
, users.fullName
, k1.who
FROM users
INNER
JOIN knows AS k1
ON users.userId=k1.whom
userId fullName who
1 Michael Miller 2
1 Michael Miller 4
2 Jefrey White 1
2 Jefrey White 4
2 Jefrey White 5
4 Linda Jenkinson 1
4 Linda Jenkinson 2
5 Dave Hoff 2
so basically this shows the friends of each user, right?
now we add the 2nd join
SELECT users.userId
, users.fullName
, k1.who
, knows.who
FROM users
INNER
JOIN knows AS k1
ON users.userId=k1.whom
INNER
JOIN knows
ON knows.whom=k1.who
userId fullName who who
1 Michael Miller 2 1
1 Michael Miller 2 4
1 Michael Miller 2 5
1 Michael Miller 4 1
2 Jefrey White 1 4
2 Jefrey White 4 1
4 Linda Jenkinson 1 4
4 Linda Jenkinson 2 1
4 Linda Jenkinson 2 4
4 Linda Jenkinson 2 5
5 Dave Hoff 2 1
5 Dave Hoff 2 4
5 Dave Hoff 2 5
so now each person’s friends are linked to their friends – this is the 2nd degree, right?
finally, let’s add the last join –
SELECT users.userId
, users.fullName
, k1.who
, knows.who
, employees.companyid
FROM users
INNER
JOIN knows AS k1
ON users.userId=k1.whom
INNER
JOIN knows
ON knows.whom=k1.who
INNER
JOIN employees
ON knows.who=employees.userId
userId fullName who who companyid
1 Michael Miller 2 1 67
1 Michael Miller 2 4 68
1 Michael Miller 2 5 67
1 Michael Miller 4 1 67
2 Jefrey White 1 4 68
2 Jefrey White 4 1 67
4 Linda Jenkinson 1 4 68
4 Linda Jenkinson 2 1 67
4 Linda Jenkinson 2 4 68
4 Linda Jenkinson 2 5 67
5 Dave Hoff 2 1 67
5 Dave Hoff 2 4 68
5 Dave Hoff 2 5 67
okay, now we’re in great shape
let’s see what the WHERE clause says, by applying the WHERE conditions to the data we’ve retrieved
employees.companyId=67 means we filter out a bunch of rows, and this is what we’re left with –
userId fullName who who companyid
1 Michael Miller 2 1 67
1 Michael Miller 2 5 67
1 Michael Miller 4 1 67
2 Jefrey White 4 1 67
4 Linda Jenkinson 2 1 67
4 Linda Jenkinson 2 5 67
5 Dave Hoff 2 1 67
5 Dave Hoff 2 5 67
now let’s apply the second condition
it says remove the rows where the 2nd degree friend is the same as the original person
this leaves us with –
userId fullName who who companyid
1 Michael Miller 2 5 67
2 Jefrey White 4 1 67
4 Linda Jenkinson 2 1 67
4 Linda Jenkinson 2 5 67
5 Dave Hoff 2 1 67
see how that works?
the employees.userId<>users.userId condition actually did apply
now, in your full query, you apply DISTINCT and of course you show only the original user, so this is what you get –
userId fullName
1 Michael Miller
2 Jefrey White
4 Linda Jenkinson
5 Dave Hoff
what was your question again???