r/learnSQL • • 9d ago

Why is my Left Join acting like an Inner Join (with no Where clause)

I've seen all the posts about people asking about joins when there's a where clause involved, but I have no idea what's causing this behavior since as far as I know it SHOULD be returning everything. This is being done in MSAccess. Please don't respond with "don't use Access" I don't have that option.

Select TableA.*, TableB.*
From TableA Left Join TableB
On (TableA.ID = TableB.ID)
And (TableA.Date = TableB.Date)

TableA and TableB have the same values for ID, but there's only one row where they both have the same date. I would expect this to give every row for TableA, and nulls in every cell for the TableB columns except for the one where there's a date match. What I'm getting instead is only the single row where there's a full match between the two tables. Am I missing something stupid here?

Edit: Since the date values keep getting brought up, both sets of dates are converted to the same type and format in previous cleaning queries. And, importantly, they are matching properly if the values exist in both tables A and B.

2 Upvotes

18 comments sorted by

7

u/nullish_ 9d ago edited 9d ago

I am speculating that there is some sort of coerced conversion happening with the date fields. What database are you working with and what are the data types for the date fields on the tables?

Edit:

That is wrong. It is because the dates are null on table b... so the comparison of tableA.date = tableb.date is UNKNOWN when date column in table b is null. You need to add an (TableA.Date = TableB.Date OR tableb.date IS NULL)

1

u/Railgun5 9d ago

I'm not sure what the database type is but I'm forcing the dates to be formatted/typed the same as part of my cleaning steps. And if the date was an issue then I don't think I'd be getting positive matches, which I am.

And the issue still mainly is that I'm not getting any of the unmatched fields to show up

4

u/Either-Surprise6390 9d ago

You said "I'm not getting any of the unmatched fields to show up"...

Let me make sure you're thinking about this right. You should get all of your records for table A, but you will NOT get all of your unmatched records in table B. You will only get records from Table B... where they match on both the ID and Date.

1

u/Railgun5 8d ago edited 8d ago

Correct. I'm only getting the records from Table A where there's a match in Table B instead of all values from Table A.

2

u/Either-Surprise6390 7d ago

Here, I'm going to help you out... I made both tables and the query. You can see TableA (3 records) and TableB (Two records) and each of their records on the upper right. Directly below that you can see the correct query result set. You can see the "Design View of the Query on the left. You can see the SQL text for the same query on the lower right. I spaced it out to make it easier to read.

Firstly, Looking at the result set... I thought of the 3 possible scenarios, each of those scenarios are show in this example. The first scenario is there is a A record with matching B record(s). The seconds scenario is there is an A record with B record(s), but they don't match up. Lastly, scenario three is there is an A record, but no B record(s) for it.

Notice the result set is correct showing all THREE of the records for TableA, and since no Table A record has multiple matching records in Table B... we get 3 rows in our dataset. The second and third scenarios still show the Table A records, but with not matching records from table B.

Each line of the result set lines up with one of the above listed scenarios, in order. Only one of the two records sync up. The first record in Table B matches on both the ID and the Dates. FYI, I had to make the dates Date-A and Date-B because "Date" is a reserved name and can't be used for fields in tables.

Now. Notice how the "Design View" that the Joins are established properly with an "Arrow" on the right side of each of them. That's showing a left join of Table A to Table B. If you don't see those arrows for each of them, they are not left joined. You can edit that in the design view by right clicking EXACTLY ON THE LINE, and selection the 2nd option as shown in the screenshot.

Here is the query code in case you'd just like to copy and paste that.

SELECT TableA.*

, TableB.*

FROM TableA

LEFT JOIN TableB ON (TableA.ID = TableB.ID)

AND (TableA.[Date-A] = TableB.[Date-B]);

Please let me know if you have any questions, comments, or concerns.

1

u/nullish_ 9d ago

I edited my original comment after I read the question again.

1

u/Railgun5 9d ago

No dice, adding the OR TableB.Date IS NULL is still not giving me all the rows.

1

u/jshine13371 8d ago edited 8d ago

The information provided, alone, wouldn't cause the issue you're experiencing. There likely is something else going on, perhaps specific to MS Access and / or your implementation in it.

You can prove what I'm saying by removing MS Access from the equation and recreating your schema and a subset of your data example in something like dbfiddle.uk and you'll see all rows returned from TableA as expected, conclusively showing this is either an MS Access issue or your implementation.

1

u/Railgun5 8d ago

Yeah my guess is it's probably and Access bug because this makes no sense otherwise, I'm mostly asking to make sure I'm not crazy.

1

u/jshine13371 8d ago

Yeah for sure. I'm also sure there's a reasonable explanation and it's not likely a bug so much as an oddity in something MS Access related (if it isn't specific to your implementation).

1

u/Discordbothost 8d ago

check whether tablea or tableb is actually a saved query with its own inner join in it - that cuts rows before your left join ever runs. also try it with only the id condition and see if the row count jumps back up.

1

u/Railgun5 8d ago

Is... that a thing? TableB is a query but I didn't think it'd force the null values to not populate at all on following queries. Plus that still wouldn't explain why I'm not getting all the values from TableA.

If I try it with just the ID match then it matches every row in table A to every row in table B, as you'd expect.

1

u/Discordbothost 8d ago

yeah, access flattens a saved query into the outer sql, so joins or criteria inside tableb can rewrite your whole join and kill the left-ness. try select * into tmpB from TableB, then left join tmpB - if all your tablea rows come back, that was it.

1

u/Ord3r2Ch4os 8d ago

I've done a basic example of this in Access and it works fine, is this your exact and full SQL for the query?

1

u/Railgun5 8d ago

Yes, apart from the names of the tables/queries I'm pulling from.

1

u/neon_beacon_sky 8d ago

If TableB is actually a query rather than a raw table it likely contains an inner join or filter that strips rows before your left join ever executes. Run the exact same SQL against a temporary physical table created from your data to rule out hidden query logic

0

u/woahboooom 8d ago

Full outer join if you want all. In op original question, same as top answer is my thoughts, date or date time is not matching. Can you state the data?

0

u/Icy-Astronaut-9994 8d ago

So my syntaxes is different, but try this, basically the same, but you can add in extra clauses.

select * from ( select * from tablea) a left outer join ( select * from tableb) b on a.id = b.id and a.date = b.date

See what shows. Also try a "full join" to help see where it's breaking.