Intelligent Enterprise | SQL Puzzlers by Joe Celko http://www.intelligententerprise.com/movabletype/blog/jcelko.html/blog/ Copyright 2010 Mon, 08 Feb 2010 13:33:41 -0500 http://www.movabletype.org/?v=3.14 http://blogs.law.harvard.edu/tech/rss Celko SQL Puzzle: Calculating Commissions I discussed a number of ways to split commissions among multiple salespeople in this article, but can you come up with other ways to keep track of the commission amounts? I'll give you a hint; ask yourself, "what is the simplest fact in this problem?"

One way of spotting a design problem is when you have to use a VIEW or CTE over and over to answer basic questions. This implies that the schema has spread information over many tables that should be in one table or that it has aggregated data in one row that should be split out into multiple rows.

]]>
http://www.intelligententerprise.com/movabletype/blog/jcelko.html/blog/archives/2008/09/celko_sql_puzzl.html /blog/archives/2008/09/celko_sql_puzzl.html Information Management Tue, 02 Sep 2008 15:10:46 -0500
Celko's Theater-Seat-Assignment SQL Puzzle In many situations, auxiliary tables are faster and more appropriate than SQL with computations (a topic I address at length this week in "Celko on SQL: Auxiliary Tables vs. Declarative Coding"). To illustrate, consider a classic problem. You have a theater and a bunch of seats you wish to sell for a performance (or think of seats for an airline flight). The seats have a sequential serial number from 1 to (n) for inventory. But in the theater building and on the tickets, the seats are arranged in rows of (k) seats and referenced by the pair (row_nbr, seat_within_row_nbr). You could construct an auxiliary table like this:

]]>
http://www.intelligententerprise.com/movabletype/blog/jcelko.html/blog/archives/2007/11/celkos_theaters.html /blog/archives/2007/11/celkos_theaters.html Information Management Mon, 05 Nov 2007 07:51:52 -0500
And the 'Email Validation' SQL Puzzle Winner Is... The winner of last week's 'Email Address Validation' SQL puzzle is "Guest" (see comments), because he/she bothered to do the research and come up with an answer that is generic enough to port to any SQL dialect with a SIMILAR TO or a regexp() function. So, "Guest" please email me with your snail mail address (and some attempt to validate your SQL mastery/identity) and I'll send you one of my books.

My answer to last week's puzzle is as follows:

]]>
http://www.intelligententerprise.com/movabletype/blog/jcelko.html/blog/archives/2007/09/and_the_email_v.html /blog/archives/2007/09/and_the_email_v.html Information Management Thu, 27 Sep 2007 08:59:13 -0500
Celko's Email-Address-Validation SQL Puzzle You've probably noticed that many Web sites use the customer's email addresses as an identifier — an email address has a very easy validation. We have all received messages something like this: "Thanks for signing up for 'The Leech Farmer's Monthly' email newsletter! We are sending you a confirmation at your email address with your temporary password."

How many ways can you write CHECK() column constraint to validate an email address? Call the column "email" just so all entries look alike (yes, you really should use "_email" to follow ISO-11179 rules). You are not allowed to do an external function call; it has to be in Standard SQL or a dialect extension.

]]>
http://www.intelligententerprise.com/movabletype/blog/jcelko.html/blog/archives/2007/09/celkos_email_ad.html /blog/archives/2007/09/celkos_email_ad.html Information Management Mon, 17 Sep 2007 07:26:12 -0500
Celko's 'Can't Be Your Own Supervisor' SQL Puzzle For this month's puzzler, consider a posting in a Newsgroup by Patrick L. Nolan at Stanford University. He has a small database with the following business rules:

1) Every person in the database is uniquely defined by a single key, their user_id.

2) Everyone is assigned a job category, call them 'A', 'B' and 'X'.

3) Everyone in job category 'X' has a supervisor who must be in either job category 'A' or job category 'B'.

4) Nobody can be their own supervisor.

One proposal was to divide job category 'X' into two, call them 'XA' and 'XB' respectively. All the 'XA' people would have 'A' supervisors, and all the 'XB' people would have 'B' supervisors.

Nolan immediately noticed that there is redundancy and the possibility of inconsistency. Suppose somebody in job category 'XA' somehow gets assigned to a supervisor in job category 'B', contrary to the definition of 'XA'. Can you think of a way to do this in pure DDL?

]]>
http://www.intelligententerprise.com/movabletype/blog/jcelko.html/blog/archives/2007/08/celkos_cant_be.html /blog/archives/2007/08/celkos_cant_be.html Information Management Mon, 20 Aug 2007 11:35:35 -0500
Cooking an old puzzle I just got an email about Puzzle #11 in my old book SQL PUZZLES & ANSWER from Rainer Gemulla at TU Dresden, Fak. Informatik, Institut SyA, in Dresden, Germany. It is a very nice cook and it is embarassing to see how needlessly complex the other answers were:

]]>
http://www.intelligententerprise.com/movabletype/blog/jcelko.html/blog/archives/2005/04/cooking_an_old.html /blog/archives/2005/04/cooking_an_old.html Mon, 25 Apr 2005 09:51:15 -0500
More on streaming databases There is a good article on streaming databases with lots of product names and stuff suitable for googling inthe current issue of INTELLIGENT ENTERPRISE.

]]>
http://www.intelligententerprise.com/movabletype/blog/jcelko.html/blog/archives/2005/04/more_on_streami.html /blog/archives/2005/04/more_on_streami.html Sat, 16 Apr 2005 10:27:25 -0500
More on streaming databases There is a good article on streaming databases with lots of product names and stuff suitable for googling inthe current issue of INTELLIGENT ENTERPRISE.

]]>
http://www.intelligententerprise.com/movabletype/blog/jcelko.html/blog/archives/2005/04/more_on_streami_1.html /blog/archives/2005/04/more_on_streami_1.html Sat, 16 Apr 2005 10:27:25 -0500
An Old Celko Puzzle If you go over to http://www.dbdebunk.com/page/page/666711.htm, you will find a letter to the editor from someone named PV about an old column of mine. Here is the jist of it:

Back in June of 1996, Jack Wells submitted this SQL problem to my SQL FOR SMARTIES column.

]]>
http://www.intelligententerprise.com/movabletype/blog/jcelko.html/blog/archives/2005/03/an_old_celko_pu.html /blog/archives/2005/03/an_old_celko_pu.html Fri, 25 Mar 2005 20:13:04 -0500
Hello everyone! INTELLIGENT ENTERPRISE once more changed format at the end of last year and my long-time column went away. However CMP asked me if I would like to do a blog on SQL, databases and the other things that interests me.

]]>
http://www.intelligententerprise.com/movabletype/blog/jcelko.html/blog/archives/2005/03/hello_everyone.html /blog/archives/2005/03/hello_everyone.html Tue, 15 Mar 2005 14:09:28 -0500