Comments on: Two subtle bugs in OUTER JOIN queries http://www.xaprb.com/blog/2010/08/02/two-subtle-bugs-in-outer-join-queries/ Stay curious! Fri, 10 May 2013 18:25:19 +0000 hourly 1 http://wordpress.org/?v=3.5.1 By: RJ http://www.xaprb.com/blog/2010/08/02/two-subtle-bugs-in-outer-join-queries/#comment-18833 RJ Wed, 27 Oct 2010 18:26:35 +0000 http://www.xaprb.com/blog/?p=1971#comment-18833 The SQL does what it says. Any resulting bugs are the direct result of the dev’s misunderstanding of what the SQL says and subsequent inappropriately use.
If you can figure out how to correct for lack of insight, please let me know! I’ll apply the patch to my system immediately.

]]>
By: Xaprb http://www.xaprb.com/blog/2010/08/02/two-subtle-bugs-in-outer-join-queries/#comment-18537 Xaprb Tue, 03 Aug 2010 20:39:43 +0000 http://www.xaprb.com/blog/?p=1971#comment-18537 I should clarify when I say “bug”. I do not mean that SQL is buggy. I mean that people write bugs in SQL just as they write bugs in C, for example,


if ( returnval = 1 ) {
format_hard_drive();
}

That’s a bug. It’s not a bug in C, though.

]]>
By: Stephen J. Fuhry http://www.xaprb.com/blog/2010/08/02/two-subtle-bugs-in-outer-join-queries/#comment-18536 Stephen J. Fuhry Tue, 03 Aug 2010 20:20:50 +0000 http://www.xaprb.com/blog/?p=1971#comment-18536 Both of these points are minor annoyances we’ve all encountered in SQL, but they really aren’t bugs — they are by design, and have several flexible workarounds.

]]>
By: John http://www.xaprb.com/blog/2010/08/02/two-subtle-bugs-in-outer-join-queries/#comment-18533 John Tue, 03 Aug 2010 15:33:29 +0000 http://www.xaprb.com/blog/?p=1971#comment-18533 I see these regularly too. The second one in particular causes the server to do a lot of extra work to do the outer join, just to discard the outer part because of the where clause.

But just to be clear, these are bugs in the QUERIES, not in SQL itself or any particular implementation of outer joins. The behavior is correct as described, but it can trip up query writers who are not aware that they act like that.

]]>
By: Anse http://www.xaprb.com/blog/2010/08/02/two-subtle-bugs-in-outer-join-queries/#comment-18530 Anse Tue, 03 Aug 2010 07:45:10 +0000 http://www.xaprb.com/blog/?p=1971#comment-18530 To overcome bug 1 you should only use non-NULL columns in the WHERE clause to be sure there is no right row. The best is always to use the same column as the joined one:

SELECT * FROM l LEFT JOIN r ON l_id = r_id WHERE r_id IS NULL;

Bug 2 can be made safe by taking any right-table-related filter into the JOIN clause itself rather than into a WHERE clause:

SELECT * FROM l LEFT JOIN r ON l_id = r_id AND r_other > 1;

]]>