Archives
 
 
 
  Special
 
 
 
  About Us
 
 
 

Newsletter
Free E-mail Newsletter from BYTE.com

 
    
           
Visit the home page Browse the four-year online archive Download platform-neutral CPU/FPU benchmarks Find information for advertisers, authors, vendors, subscribers Request free information on products written about or advertised in BYTE Submit a press release, or scan recent announcements Talk with BYTE's staff and readers about products and technologies

ArticlesRight Syntax, Wrong Result: How SQL Can Go Awry


February 1995 / Reviews / Make Bulletproof SQL Queries / Right Syntax, Wrong Result: How SQL Can Go Awry

Esperant's SQL Expert is designed to prevent you from making choices that, though syntactically correct, violate the underlying logic of the query, producing inaccurate reports. Software AG offers the following scenario to illustrate how such a mistake can happen.

Start with a database that consists of the following three tables for customer name, orders, and order detail:


Creating a Query

illustration_link (16 Kbytes)

To create a query that returns each customer's total number of orders and order dollars, most query tools would allow a point- and-click query that generates the underlying SQL SELECT statement below. The database would perform the required joins to construct the following (undisplayed) table:


Resulting Table

illustration_link (9 Kbytes)

   SELECT T1.CNAME,SUM(T2.ORDER$),
       SUM(T3.QTY)
    FROM CUSTOMER T1, ORDER T2,
       ORDER_DETAIL T3
    WHERE T1.CUST# = T2.CUST#
      AND T2.ORD# = T3.ORD#
    GROUP BY T1.CNAME


Summing

illustration_link (5 Kbytes)

SQL would then do the summing against the internal table, displaying this report. The SUM(ORDER$) results, though they look fine at first glance, are actually incorrect. That's because ORDER$ has been duplicated for each Order Detail record, causing it to be overstated when the summing is performed. Accor ding to Software AG, only a user with a good grasp of SQL theory would have known that summing at two levels of detail in the same SELECT statement would produce erroneous results. Unlike most query tools, Esperant's SQL Expert would have grayed out the QTY column the instant you chose to SUM the ORDER$ column. The query would produce the following SQL code along with the correct report table:


Esperant's Expert Solution

illustration_link (5 Kbytes)

   SELECT T1.CNAME, SUM(T2.ORDER$)
    FROM CUSTOMER T1, ORDER T2
    WHERE T1.CUST# = T2.CUST#
    GROUP BY T1.CNAME


Up to the Reviews section contentsGo to previous article: Make Bulletproof SQL Queries Go to next article: Simple, Scalable RAIDSearchSend a comment on this articleSubscribe to BYTE or BYTE on CD-ROM  
Flexible C++
Matthew Wilson
My approach to software engineering is far more pragmatic than it is theoretical--and no language better exemplifies this than C++.

more...

BYTE Digest

BYTE Digest editors every month analyze and evaluate the best articles from Information Week, EE Times, Dr. Dobb's Journal, Network Computing, Sys Admin, and dozens of other CMP publications—bringing you critical news and information about wireless communication, computer security, software development, embedded systems, and more!

Find out more

BYTE.com Store

BYTE CD-ROM
NOW, on one CD-ROM, you can instantly access more than 8 years of BYTE.
 
The Best of BYTE Volume 1: Programming Languages
The Best of BYTE
Volume 1: Programming Languages
In this issue of Best of BYTE, we bring together some of the leading programming language designers and implementors...

Copyright © 2005 CMP Media LLC, Privacy Policy, Your California Privacy rights, Terms of Service
Site comments: webmaster@byte.com
SDMG Web Sites: BYTE.com, C/C++ Users Journal, Dr. Dobb's Journal, MSDN Magazine, New Architect, SD Expo, SD Magazine, Sys Admin, The Perl Journal, UnixReview.com, Windows Developer Network