tags:

views:

15

answers:

1

Hello, I am running 2 queries against an informix database. the queries are hitting 4 different tables with both inner an outer joins. I thought they should both return the same result set, but I am getting a 3 record difference. below are the 2 queries -

query 1 (returns 65 rows) -

 select ...  
    from table1, table2, outer table3, outer table4
   where table1.id = table2.id
     and table1.id = table3.id
     and table1.id = table4.id 
     and .....

query 2 (returns 62 rows) -

select ....
from table1 inner join table2 on table1.id = table2.id
left outer join table3 on table1.id = table3.id
left outer join table4 on table1.id = table4.id
where .....

Does anyone have any idea why these 2 return different result sets? I assumed that by changing to use inner join / left outer join instead of just using outer (and nothing for the inner join) would return the same results.

+1  A: 

The contents of the WHERE clauses are probably a factor in this. The first notation, the Informix-style OUTER join notation, is non-standard, and has rather peculiar semantics (that's being polite). The second notation is the ISO standard OUTER JOIN which has prescribed semantics.

With the Informix-style join, all the rows from the inner-joined pair of tables that survive any filters in the WHERE clause that are on their columns only will be returned in the result set. If there are filters in the WHERE clause on columns in the outer-joined tables, or between the inner tables and the outer tables, then those filters may eliminate some possible rows from the result set, but the innner-joined part of the result will still appear extended with nulls. It is excruciatingly complex to explain. It sort of more or less makes sense, but the result isn't simply a projection of a restriction of the basic set of joins.

Can you confirm that the WHERE clauses contain some conditions on columns in table3 and table4?

There isn't a trivial way to simulate the Informix-style OUTER join with ISO standard OUTER JOIN notation.

Jonathan Leffler
Yes, there are conditions on the columns in table3 and table4. What you explained is most certainly happening. I am just going to have to try to stick with the original query. Am I correct in assuming that the first query is the most correct?
czuroski
@czuroski: "Is the first query most correct?" They are simply different queries - and you need to know which one produces the answer you need. Given that your application was originally written using the Informix-style join, that probably gives the correct result for your application - and therefore is the better choice. However, you could modify your application to work with the ISO (standard) joins; you'd probably rephrase the query slightly differently, that's all. I haven't worked out whether there is a way to achieve the Informix-style result with ISO joins, but I know it ain't trivial.
Jonathan Leffler