Welcome ...        
          
  ... Visiting Newsgroup Users!
 
  See "What makes SQLA different from the newsgroups?" in the FAQ.
 
vote up 2 vote down
star

I have just searched through StackOverflow but couldn't find much...

To all the SQL experts,

is there anything comparable to the "design patterns movement" which has taken place in the programming language world since 1994? You know, the "Gang of Four" book and the like.

I'm asking because I feel the need to structure/organize typical SQL constructs for educational purposes. The question is focussing primarily on querying, not on data modeling.

These possible patterns might classify

  • when to use a correlated subquery vs. a join,
  • when to use a derived table,
  • when to use an union vs. a disjunction

and the like. Such rules of thumb may be something like

  • If you want to get the rows with the maximal column value c of all rows in table T, use a join on a derived query with group by max as select T.* from T inner join (select ...)...

(I don't claim this is a valid solution, it's just the way I would like a sample to be.)

And I expect them to be general approaches though the possible solutions will obvioulsy depend on the features of the SQL engine - i.e. are WINDOW functions available). In that respect, I would prefer solutions working with SA 11.0.1 and above:)

Any hints are highly appreciated!


[Just do clarify: Inspite of the heavy usage of "like" and "pattern" in this question, I am not at all refering to pattern matching:)]

flag
No personal offense intended, but I'm tempted to create the tag "architecture-astronaut"... :) – Breck Carter Feb 3 at 14:36
Seriously, why don't you post some individual questions, like "When should a correlated subquery be used instead of a join, and vice versa?" – Breck Carter Feb 3 at 14:37
1 
Um... try googling "sql design patterns" – Breck Carter Feb 3 at 14:39
Dear Breck, I appreciate your comment, but I am really interested whether users of this fine website have any hints to helpful books on these topics, or what iAnywhere experts have to say. It's definetely not an individual question I have but the general question if there is some valuable collection of such questions/answers. - And I try to keep grounded:) – Volker Barth Feb 3 at 17:13
1 
FWIW I have a fairly large number of what I call "templates"... snippets of SQL in text files arranged by meaningful file and folder names, which I use all the time in my own work (and often copy and paste into forum answers). I imagine that some other folks might have the same thing, a personal "toolkit". I have often thought about publishing them, but that would be a large effort... much cleanup would be required, and the only true motivators (fear and greed) are missing :) – Breck Carter Feb 3 at 20:10
show 2 more comments

1 Answer

vote up 3 vote down
check

In a word, the answer is "yes" though I think many references merely scratch the surface. As one example, a relatively new book by Vadim Tropashko offers solutions to the following patterns:

  • Counting
  • Conditional summation
  • Integer generator
  • String/Collection decomposition
  • List Aggregate
  • Enumerating pairs
  • Enumerating sets
  • Interval coalesce
  • Discrete interval sampling
  • User-defined aggregate
  • Pivot
  • Symmetric difference
  • Histogram
  • Skyline query
  • Relational division
  • Outer union
  • Complex constraint
  • Nested intervals
  • Transitive closure
  • Hierarchical total

but as useful as these patterns may be, to me these are merely a part of the problem. In my view, "Design Patterns" with relational databases must include both logical and physical schema design since there are always tradeoffs between the SQL constructions one might use and aspects of the physical schema that render the queries (or updates) possible (or not), and what their performance characteristics may be.

link|flag
1 
Some more information and reviews: <amazon.com/SQL-Design-Patterns-Programming-Focus/…; – Breck Carter Feb 6 at 12:02
Thanks for the pointer - I'm gonna have a look at that, as the book seems to be available here, too:) – Volker Barth Feb 8 at 8:36
@Glenn: I agree with your point of view that designing queries can't be separated from the schema design. - But sometimes you have to work with an already fixed schema (of a 3rd party vendor's application), And that's what I am dealing with currently:) – Volker Barth Feb 8 at 8:40

Your Answer

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