Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Left join together with inner join in ms-access

I have to run the following query in an access database:

    SELECT School.Name, Student.Name, Grade.Grade
    FROM( (`School` `School` LEFT JOIN `Student` `Student` ON `School`.`ID`=`Student`.`SchoolID`) INNER JOIN `Grade` `Grade` ON `Student`.`GradeID` = `Grade`.`ID`)

This gives me the error "Join expression not supported", but if I change the LEFT JOIN to INNER JOIN, it runs correctly. I think the way of using both these joins together on ms-access can have a different way. Can anyone pls tell me what is wrong in this query and/ or the reason that this is not supported.

Thanks

like image 359
Dulini Atapattu Avatar asked Apr 20 '11 03:04

Dulini Atapattu


People also ask

Can we use left outer join and inner join together?

If they are at the same 'scope', the INNERS will always run, you can't conditionally have them run based on their order. You either have to use two LEFT joins and filter out the records you don't want, or alternatively use a View to scope the INNER JOIN.

Is it possible for left join and inner join to produce the same results?

The reason why LEFT JOIN and INNER JOIN results are the same is because all the records of table branch has at least one match on table user_mast . The main difference between INNER JOIN and LEFT JOIN is that LEFT JOIN still displays the records on the the LEFT side even if they have no match on the RIGHT side table.

How do you use left join as inner join?

You'll use INNER JOIN when you want to return only records having pair on both sides, and you'll use LEFT JOIN when you need all records from the “left” table, no matter if they have pair in the “right” table or not.

Does MS Access Support LEFT join?

Remarks. Use a LEFT JOIN operation to create a left outer join. Left outer joins include all of the records from the first (left) of two tables, even if there are no matching values for records in the second (right) table. Use a RIGHT JOIN operation to create a right outer join.


Video Answer


2 Answers

I re-created your query in Access' query designer. The query worked with both joins as INNER, as you found. However, the query designer complained about "ambiguous outer join" when I tried to switch the first INNER JOIN to LEFT JOIN. LEFT for both joins works, as does INNER for both.

If you need LEFT for the first join and INNER for the second, you can move the tables from the second join into a subquery.

SELECT
    School.[Name] AS school_name,
    sub.[Name] AS student_name,
    sub.Grade
FROM
    School LEFT JOIN (
        SELECT
            Student.SchoolID,
            Student.[Name],
            Grade.Grade
        FROM
            Student INNER JOIN Grade
            ON Student.GradeID = Grade.ID) AS sub
    ON School.ID = sub.SchoolID;
like image 63
HansUp Avatar answered Oct 05 '22 10:10

HansUp


Student.GradeID might be null, because you did a LEFT JOIN on Student, it might not exist for your second join.

So basically, whenever you do a LEFT JOIN, you cannot use any of those TABLES columns in future JOINS

like image 32
Aducci Avatar answered Oct 05 '22 10:10

Aducci