0

I have a table of students. and a table of teachers.

SOME of the students (not all) will have a teacher assigned to them. this is controlled in a 3rd table, matching those students with their teachers, via the studentID and teacherID

what i need the SQL to do, is to LEFT OUTER JOIN onto the 3rd table, which is then INNER JOINED onto the teacher's table (because not all students will appear in the 3rd table, but any teacher that appears in the 3rd table WILL appear in the teachers table).

i am looking to get a result of all student names, and teacher's name, where they are assigned (and null if not).

what i have so far looks like this, and it basically operates as a full INNER JOIN, and does not give me students who do not have teachers assigned:

SELECT firstname, teacherlastName
FROM tblstudents 

left outer join [tblStudentRakazot] 
ON tblstudents.studentid = [tblStudentRakazot].studentID 

INNER JOIN tblteachers 
ON [tblStudentRakazot].teacherid = tblteachers.teacherID

can someone pls give me a pointer here? i tried with placing brackets, but that didn't see, to help.

thanks!

5 Answers 5

1

You don't use an INNER JOIN but only another LEFT JOIN.

Think of tblStudents as your base. You want to get all of them, not filter anything out, and only attach optional info. With the first left join, you attach a first info
Student -> TeacherAssignment
The TeacherAssignment can be null or not null. Now you only attach another info - the teacher's full name pulled from tblTeachers.
Student -> TeacherAssignnent -> TeacherName
Do this with another LEFT JOIN. That attaches the info, where possible, i.e. where TeacherAssignment is not null. This ignores rows where TeacherAssignment is null anyway.

Sign up to request clarification or add additional context in comments.

Comments

1
SELECT firstname, teacherlastName
FROM tblstudents 
left outer join
( select * from
 [tblStudentRakazot] A INNER JOIN tblteachers B 
ON A.teacherid = B.teacherID)AS C
ON tblstudents.studentid = C.studentID 

1 Comment

thanks for this, you answer was very helpful, because it enabled me to use a WHERE clause on the inner SELECT
1

You could move your INNER JOIN to a subquery

SELECT  firstname, teacherlastName
FROM    tblstudents 
        LEFT OUTER JOIN
        (   SELECT  [tblStudentRakazot].studentID, tblTeachers.teacherlastName
            FROM    [tblStudentRakazot]
                    INNER JOIN tblteachers 
                        ON [tblStudentRakazot].teacherid = tblteachers.teacherID
        ) teachers
            ON tblstudents.studentid = teachers.studentID 

Another option is to use a more complicated where clause.

SELECT  firstname, teacherlastName
FROM    tblstudents 
        LEFT JOIN [tblStudentRakazot] 
            ON tblstudents.studentid = [tblStudentRakazot].studentID 
        LEFT JOIN tblteachers 
            ON [tblStudentRakazot].teacherid = tblteachers.teacherID
WHERE   [tblStudentRakazot] IS NULL
OR      tblteachers.teacherID IS NOT NULL

However, SQL Server is pretty good at propogating predicates out of subqueries where it needs to, so I would favour the first approach for both readabilty and efficiency.

EDIT

I did not read the question properly, I thought you did not want records where the teacherID in tblStudentRakazot was NULL. If this is not an issue then you can simply use two LEFT JOINS, without the where clause as above:

SELECT  firstname, teacherlastName
FROM    tblstudents 
        LEFT JOIN [tblStudentRakazot] 
            ON tblstudents.studentid = [tblStudentRakazot].studentID 
        LEFT JOIN tblteachers 
            ON [tblStudentRakazot].teacherid = tblteachers.teacherID

2 Comments

I'm starting to wonder if I've misunderstood something: He just wants the Students and the assigned teachers or null for the teacher. There should be no need for any WHERE part at all in your second query...
@KekuSemau I did not read the question properly, I thought the OP did not want records where the teacherID in tblStudentRakazot was NULL, which is why I thought the where clause was required, you are right, it isn't required.
0
SELECT b.student_firstname, teacherlastName
FROM thirdtable a 
left join studenttbl b on a.studentid = b.studentid
left join teachertbl b on a.teacherid = b.teacherid

You can always use outer join if you are certain data in teacher table is unique. it will give same result as inner join.

Comments

0

You may not use a subquery as in @GarethD sample.

SELECT firstname, teacherlastName
FROM tblstudents
LEFT OUTER JOIN [tblStudentRakazot]
    INNER JOIN tblteachers
        ON [tblStudentRakazot].teacherid = tblteachers.teacherID
    ON tblstudents.studentid = [tblStudentRakazot].studentID

But when looking more deeply execution plans of this query and query with subquery will likely be equivalent.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.