EDIT Here is my problem simplified
I need data from Sales and References table
Sales returns me data with amount transfered
References returns me data with the amount to supply and is filetered by inventory
Basically i need to get the results that match References but i need to also add the results that do not appear on the References table but these are ignored by the where clause
Here is the actual updated query i used in the beginning
SELECT
A.Reference,
B.QtyToSupply,
A.QtyTransfered
FROM
SALES A
RIGHT JOIN
REFERENCES B ON A.Reference = B.Reference
WHERE B.InventoryId = 1
UPDATE EXAMPLE
SELECT
Reference,
QtyTransfered
FROM
SALES
This returns this data:
Reference | QtyTransfered
M1 200
M1 200
M2 200
M4 500
M5 250
M6 300
The joined query returns this data
SELECT
A.Reference,
B.QtyToSupply,
A.QtyTransfered
FROM
SALES A
RIGHT JOIN
REFERENCES B ON A.Reference = B.Reference
WHERE B.InventoryId = 1
Reference | QtyToSupply | QtyTransfered
M2 200 200
M4 500 500
M6 300 300
M9 800 NULL
M10 800 NULL
With left join
Reference | QtyToSupply | QtyTransfered
M2 200 200
M4 500 500
M6 300 300
The output should be
Reference | QtyToSupply | QtyTransfered
M2 200 200
M4 500 500
M6 300 300
M9 800 NULL
M10 800 NULL
M1 NULL 200
M1 NULL 200
M5 NULL 250
FINAL RESULT
Here is how i managed to get the result intended despite the confusion since i haven't ever used union before.
SELECT
A.Reference,
B.QtyToSupply,
A.QtyTransfered
FROM
SALES A
RIGHT JOIN
REFERENCES B ON A.Reference = B.Reference
WHERE B.InventoryId = 1
UNION
SELECT
S.Reference,
R.QtyToSupply,
S.QtyTransfered
FROM
SALES S
WHERE NOT EXISTS ( SELECT 1 FROM References R WHERE R.Reference= S.Reference)
.I'll edit the question later to look less confusing for people who go through the same problem