Please be aware that the SAP SQL Anywhere Forum will be shut down on August 29th, 2024 when all it's content will be migrated to the SAP Community.

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:)]

asked 03 Feb '10, 10:44

Volker%20Barth's gravatar image

Volker Barth
accept rate: 34%

edited 03 Feb '10, 14:24

No personal offense intended, but I'm tempted to create the tag "architecture-astronaut"... :)

(03 Feb '10, 14:36) Breck Carter

Seriously, why don't you post some individual questions, like "When should a correlated subquery be used instead of a join, and vice versa?"

(03 Feb '10, 14:37) Breck Carter

Um... try googling "sql design patterns"

(03 Feb '10, 14:39) Breck Carter

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:)

(03 Feb '10, 17:13) Volker Barth

Just to add: There seem to be some collections of particular problems when using certain types of queries in Oracle, MS SQL and the like, cf. the site ** I don't know if there is something comparable focussed on SA (besides the NGs and this site). As stated, it's more for educational purposes*, not to find a way how to formulate one particular query. For the latter, I would (and will!) ask that particular question here.

(03 Feb '10, 17:20) Volker Barth

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 :)

(03 Feb '10, 20:10) Breck Carter

...and besides, "templates" fall far short of "design patterns" even on a good day :)

(03 Feb '10, 20:12) Breck Carter
More comments hidden
showing 5 of 7 show all flat view

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.

permanent link

answered 05 Feb '10, 20:41

Glenn%20Paulley's gravatar image

Glenn Paulley
accept rate: 43%

Replies hidden
Comment Text Removed

Thanks for the pointer - I'm gonna have a look at that, as the book seems to be available here, too:)

(08 Feb '10, 08:36) Volker Barth

@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:)

(08 Feb '10, 08:40) Volker Barth

Dang! After over a year, you would think they would have some used copies in the $20 range. No chance. This must be a great book. You're still paying over $50 for a used one. :)

Jeff Gibson
Intercept Solutions - Sybase SQL Anywhere OEM Partner
Nashville, TN

(13 May '11, 16:50) Jeff Gibson
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:


question asked: 03 Feb '10, 10:44

question was seen: 3,511 times

last updated: 13 May '11, 16:50