2

Technologies: VS.net 2008, C#, Winforms, SQL Server 2008

I'm currently in the process of writing a Visual Select Query builder for work, but I'm stuck on how joins are created. I understand how I need to do them when I write them manually, but I'm not sure how I can translate that to an algorithm.

Currently, my select query builder has all the functionality of getting tables, displaying columns, choosing what columns you want, using "AND, OR" statements on specific columns and it runs the query just fine. But once another table is involved, all hell breaks loose.

Using an already existing application is out of the question and answers that suggest other products should be downvoted.

Is there a hard and fast rule that's says "When writing a Join statement, you should be doing __ and __, etc...)

I've based my initial concept on MS Access, and I noticed it uses all left Joins, but I don't see why it does that.

4
  • Maybe I am not getting your question, but: when writing a join statement, you should be selecting the tables you need, the columns to use in the join and what kind of join you use. there is not more to it.
    – Jacob
    Jul 14, 2011 at 16:41
  • 2
    MS Access does not use "all left joins". Jul 14, 2011 at 16:41
  • The hard rules would be stated in a grammar for SQL. such as this
    – nos
    Jul 14, 2011 at 16:48
  • not too direct to the question but something like sqlkata.vivida-apps.com/docs/join may help you
    – amd
    Aug 31, 2017 at 12:23

2 Answers 2

1

Basic criteria you will want to know:

  • What Field(s) make the tables related?
  • What fields have inequivalencies?
  • Do you want to limit the number of results to matches only, or include non-matches?

The first criteria is important for your ON statement. So is the second. It's not uncommon to have a query where you want to see results that have some fields in common but others NOT, such as:

SELECT a.LineId, a.Total, b.Subtotal
FROM TableA as A
INNER JOIN TableB as B
    ON b.subtotal = a.total
    AND b.ID <> a.LineId

The third criteria is how you know if you want a LEFT or INNER JOIN.

If you want only to see only matching rows, use INNER. If you want to see all rows from the first table, but show a match from the second when it exists, use a LEFT JOIN.

You may also want to consider FULL OUTER joins, which show all results from both tables. CROSS JOINs aren't as widely used, so you will need to determine if you have a use case for those. They show the cartesian product (i.e. all rows from tableA matched with all rows from TableB - all possible combinations).

Can you be more specific in what you need?

0
1

There are no "hard and fast" rules for this kind of thing because it depends very heavily on the structure of the data. I think you're off to a good start watching how MS Access handles it. Other good options would be to create some sample Entity Framework or LINQ 2 SQL models in the designer, and observe how those are translated into SQL on the back end. (EF's join designer, in particular, is pretty smart and flexible).

Access uses primarily left joins because they are the "safest" given absolutely zero knowledge of the source data structure. The trick is to try and design a tool that matches what your user is expecting. In Access's query designer, if I select a table, then connect it to another table, the most likely scenario is "I want all the data from this table but I need to pull it data from that other table too", which is a left join. If an inner join was produced and I ended up not getting all of the rows in the first table back, that would probably be surprising.

Of course, Access also allows you to fix those rules if you really need to. That's the best approach: produce a sensible default that is least likely to confuse your user, then provide them a way to change the default if they know better.

One option would be to translate the join language into something slightly higher level; for example, if your users are familiar with data modelling at all they may recognize "one-to-many" (inner join) vs. "one-to-zero-or-more" (outer join). Possibly even make the operation that creates joins in your builder use completely different words, such as "Optional Link" vs. "Required Link" or even "Connect Lookup Table" vs. "Merge Child Data". Find out what words or terms your users think in their heads when they use your query designer, map those to appropriate JOIN types, and use those. Again, the EF designer has mapped the SQL concept of joins onto a higher-level data modelling concept of parent/child relationships that works well.

From a technical perspective, there are my personal rules about writing JOINs. Some of them are based on possibly outdated or obsolete ideas of how to optimize queries and indexes, and may no longer be strictly needed, but have served me well:

  • Always use the long-form join syntax (no WHERE clause short-cuts
  • Put all of the criteria on the child table rows into the ON clause when possible (that is, my WHERE clauses rarely have conditions that include fields from inner-joined tables)
  • Use an INNER JOIN where is it known to be safe
  • Prefer LEFT JOINS and IS NOT NULL to subqueries when possible.

EDIT: I've been corrected on my incorrect perception that JOINS perform faster than sub-queries. (Of course, you were going to do your own performance tests anyway, right? :) )

3
  • FYI, subqueries with IN or EXISTS perform much better as a rule than LEFT JOIN...(NOT) NULL in SQL Server 2005+
    – JNK
    Jul 14, 2011 at 17:08
  • I admit I haven't actually benchmarked the difference since MSDE was in vogue. Thanks for the heads up. Jul 14, 2011 at 17:25
  • No problem. Gail Shaw did a great blog post on this a while back: sqlinthewild.co.za/index.php/2010/03/23/…
    – JNK
    Jul 14, 2011 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.