简体   繁体   中英

SQL exercise using a self Left Outer Join

This is the exercise: How can you output a list of all members, including the individual who recommended them (if any)? Ensure that results are ordered by (surname, firstname).

The solution is below only I don't understand why it's 'ON recs.memid = mems.recommendedby' and not 'mems.memid = recs.recommendedby'. Why doesn't the latter work? I want to correct my thinking on how to use a Left Outer Join to itself.

  CREATE TABLE members (
  memid SERIAL PRIMARY KEY,
  firstname VARCHAR(20),
  surname VARCHAR(20),
  recommendedby INTEGER References members(memid)
  );

  SELECT
  mems.firstname AS memfname, 
  mems.surname AS memsname, 
  recs.firstname AS recfname, 
  recs.surname AS recsname
FROM
  cd.members AS mems
LEFT OUTER JOIN cd.members AS recs
ON recs.memid = mems.recommendedby
ORDER BY memsname, memfname;

Consider this data:

MEMID | FIRSTNAME | SURNAME | RECOMMENDEDBY
------|-----------|---------|--------------
1     | John      | Smith   | null
2     | Karen     | Green   | 1
3     | Peter     | Jones   | 1

Here John recommended both Karen and Peter, but no one recommended John


'ON recs.memid = mems.recommendedby' (The one that "works")

You're getting a list of members and the ones that recommended them. Any member can only have been recommended by one member as per the table structure, so you'll get all the members just once. You're taking the recommendedby value and looking for it in the memid column in the "other table":

recommendedby   --|-->  memid of members that recommended them
MEMID | FIRSTNAME | SURNAME | RECOMMENDEDBY
------|-----------|---------|--------------
Karen | Green     | John    | Smith
Peter | Jones     | John    | Smith
John  | Smith     | null    | null

The recommendedby column only has John (1), so when looking for the value 1, John comes up.


'ON mems.memid = recs.recommendedby' (The one that doesn't work)

You'll again get all the members. But here you're getting them as the ones doing the recommending, so to say. If they didn't recommend anyone, the paired record will be blank. This is because you're taking the memid value and looking to see if it matches the recommendedby column of the "other table". If a member recommended more than one, the record will appear multiple times:

memid           --|-->    recommendedby
MEMID | FIRSTNAME | SURNAME | RECOMMENDEDBY
------|-----------|---------|--------------
Karen | Green     | null    | null
Peter | Jones     | null    | null
John  | Smith     | Karen   | Green
John  | Smith     | Peter   | Jones

Karen and Peter didn't recommend anyone, but John recommended both the others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM