r/learnSQL • u/Railgun5 • 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.
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/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
TableAas expected, conclusively showing this is either an MS Access issue or 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/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/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.