简体   繁体   中英

LEFT OUTER JOIN with INNER JOIN

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!

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

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, ie where TeacherAssignment is not null. This ignores rows where TeacherAssignment is null anyway.

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 
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.

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.

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