Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write a full outer join query in access

Tags:

sql

ms-access

Original query:

SELECT *  FROM AA FULL OUTERJOIN BB on (AA.C_ID = BB.C_ID);   

How do I convert the query above to make it compatible in Microsoft Access?

I am assuming:

SELECT * FROM AA FULL LEFT JOIN BB ON (AA.C_ID = BB.C_ID); 

I haven't dealt with the "FULL" criteria before am I correctly converting the first query into a query compatible with Access?

like image 440
user2924488 Avatar asked Oct 27 '13 06:10

user2924488


People also ask

What is the command of full outer join?

The FULL OUTER JOIN keyword returns all records when there is a match in left (table1) or right (table2) table records. Tip: FULL OUTER JOIN and FULL JOIN are the same.

What is full outer join explain with example?

What is Full Outer Join in SQL? In SQL the FULL OUTER JOIN combines the results of both left and right outer joins and returns all (matched or unmatched) rows from the tables on both sides of the join clause.


2 Answers

Assuming there are not duplicate rows in AA and BB (i.e. all the same values), a full outer join is the equivalent of the union of a left join and a right join.

SELECT *     FROM AA         LEFT JOIN BB ON AA.C_ID = BB.C_ID UNION SELECT *     FROM AA         RIGHT JOIN BB ON AA.C_ID = BB.C_ID 

If there are duplicate rows (and you want to keep them), add WHERE AA.C_ID IS NULL at the end, or some other field that is only null if there is not corresponding record from AA.

EDIT:

See a similar approach here.

It recommends the more verbose, but more performant

SELECT *     FROM AA         JOIN BB ON AA.C_ID = BB.C_ID UNION ALL SELECT *     FROM AA         LEFT JOIN BB ON AA.C_ID = BB.C_ID     WHERE BB.C_ID IS NULL UNION ALL SELECT *     FROM AA         RIGHT JOIN BB ON AA.C_ID = BB.C_ID     WHERE AA.C_ID IS NULL 

However, this assumes that AA.C_ID and BB.C_ID are not null.

like image 192
Paul Draper Avatar answered Sep 28 '22 19:09

Paul Draper


The more eficient and faster code:

SELECT *     FROM AA         LEFT JOIN BB ON AA.C_ID = BB.C_ID UNION ALL SELECT *     FROM AA         RIGHT JOIN BB ON AA.C_ID = BB.C_ID     WHERE AA.C_ID IS NULL 
like image 37
user6012447 Avatar answered Sep 28 '22 19:09

user6012447