-1

I have four different tables

class(classID, className)
person(personID, name)
schedule(personID, classID)
enrollment(personID, grade)

What is the easiest way to get each distinct column in one table? I understand that I would start with enrollment, get the personID and grade, add them to the result table, then use the personID to get the name as well as the classID, and then use the classID to get the className. I just don't know exactly how to do that.

  • 1
    Hint: `JOIN` and `DISTINCT`. – Eric Jul 31 '17 at 22:18
  • Also it's either `MySQL` or `SQL Server`, can't be both? Which one is it? Remove the unnecessary tag. – Eric Jul 31 '17 at 22:19
  • Possible duplicate of [SQL SELECT from multiple tables](https://stackoverflow.com/questions/1416003/sql-select-from-multiple-tables) – Simon Jul 31 '17 at 22:59
  • It's unclear what you want. But try this: `SELECT * from class NATURAL JOIN schedule NATURAL JOIN person NATURAL JOIN enrollment`. – Paul Spiegel Jul 31 '17 at 23:18

1 Answers1

0

It seems that you need something like this:

SELECT p.name personName, e.grade, c.className
    FROM person p
    JOIN enrollment e ON e.personId = p.personId
    JOIN schedule s ON s.personId = p.personId
    JOIN class c ON c.classId = s.classId
    WHERE p.personId = 1;
Ruslan K.
  • 1,912
  • 1
  • 15
  • 18