fhg
Volume 7, Number 18 -- May 9, 2007

WHERE Versus HAVING

Published: May 9, 2007

Hey, Ted:

What is the purpose of SQL's HAVING operator? It seems to me to be unnecessary, since the WHERE operator can choose which records are to be included in the query. What am I missing?

--Rich


Don't sweat it, Rich. This is a very common point of confusion. Let me try to straighten it out for you, and in the process I'll add a tip that some of the more experienced readers may not have seen before.

Sometimes you can get the same result either WHERE or HAVING. The following two SQL commands produce the same result set. That is, both count the number of records found for the states of Texas and Georgia.

SELECT state, COUNT(*)
  FROM qiws/qcustcdt
 WHERE state IN ('GA', 'TX')
 GROUP BY state
 ORDER BY state

SELECT state, COUNT(*)
  FROM qiws/qcustcdt
 GROUP BY state
HAVING state IN ('GA', 'TX')
 ORDER BY state

So, what's the difference? Which is better? I'll let you answer those questions in a minute.

The purpose of the WHERE clause is to select rows (records) that are to be included in the query. For example, assume table QCUSTCDT, which you can find on your system in the QIWS library. Suppose I want the names, account numbers, and balance due of all customers from Texas and Georgia. Since STATE is one of the fields in the record format, I can use WHERE to select those customers.

SELECT cusnum, lstnam, init
  FROM qiws/qcustcdt
 WHERE state IN ('TX', 'GA')

CUSNUM LSTNAM       INIT   BALDUE
====== ============ ==== ========
938472 Henning      G K     37.00
938485 Johnson      J A   3987.50 
593029 Williams     E D     25.00

Suppose I want the total amount due from customers by state. In that case, I would need to use the GROUP BY clause to build an aggregate query.

SELECT state,SUM(baldue)
  FROM qiws/qcustcdt
 GROUP by state
 ORDER BY state

State Sum(Baldue)
===== ===========
CA         250.00
CO          58.75 
GA        3987.50 
MN         510.00
NY         589.50
TX          62.00 
VT         439.00 
WY            .00

Now, suppose I want the same information, but I don't care about states where nobody owes me any money. Since the total owed by state is an aggregate figure, i.e., the figure is generated from a group of records, you must use HAVING to select the proper data.

SELECT state,SUM(baldue)
  FROM qiws/qcustcdt
 GROUP by state
HAVING SUM(baldue) > 0
 ORDER BY state

State Sum(Baldue)
===== ===========
CA         250.00
CO          58.75 
GA        3987.50 
MN         510.00
NY         589.50
TX          62.00 
VT         439.00  

Notice that Wyoming is no longer in the picture.

Here's the rule. If a condition refers to an aggregate function, put that condition in the HAVING clause. Otherwise, use the WHERE clause.

Here's another rule: You can't use HAVING unless you also use GROUP BY.

Now, go back to the first example, where WHERE and HAVING produce the same result set. What's the difference? The first query uses the WHERE clause to restrict the number of rows that the computer has to sum up. But the second query sums up all the rows in the table, then uses HAVING to discard the sums it calculated for all states except Texas and Georgia. The first query is obviously the better one, because there is no need to make the computer calculate sums and then throw them away.

I mentioned a tip that some of the more experienced readers may not have seen. Some queries combine aggregate processing with non-aggregate processing. In a classic example, one table contains individual sales, while another contains a quota for a sales rep. The first table contains more than one record per rep, and those records must be summarized in order to get an aggregate total, which is then compared to the sole quota record for a rep. How do we find the reps who have not met their quotas?

Like this:

SELECT rep, SUM(amount)
  FROM sales AS a
 GROUP by rep
HAVING SUM(a.amount) >= 
           (SELECT quota
              FROM quotas AS b
             WHERE b.rep = a.rep)
 ORDER BY rep

The first SELECT sums the individual sales by rep. Each rep's sum is compared to one record from the QUOTAS table, in order to determine if the aggregate sum is at least as much as the quota. As before, it's necessary to put the aggregate function (SUM) in the HAVING clause.

If these are the sales:

Rep  Amount
===  ======
1      20 
1      30 
2      40 
3      30
3      20

And these are the quotas:

Rep  Quota
===  =====
1      70 
2      30 
3      40

This is the result:

Rep SUM(Sales)
=== ==========
2	40 
3	50

How about that? A nested query in the HAVING clause.

--Ted



                     Post this story to del.icio.us
               Post this story to Digg
    Post this story to Slashdot


Sponsored By
PRODATA COMPUTER SERVICES

Remote Database Access!

DBU RDB allows you to connect to your MySQL, SQL, DB2
and Oracle databases remotely from your System i!

Team it up with DBU Audit to capture all adds, changes and deletes made with DBU.

Contact us today at 800.228.6318 or sales@prodatacomputer.com
for your FREE 30-day licensed copy.

www.prodatacomputer.com


Senior Technical Editor: Ted Holt
Technical Editors: Howard Arner, Joe Hertvik, Shannon O'Donnell, Kevin Vandever
Contributing Technical Editors: Joel Cochran, Wayne O. Evans, Raymond Everhart,
Bruce Guetzkow, Brian Kelly, Marc Logemann, David Morris
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

Sponsored Links

Quadrant Software:  Become more efficient, productive, & profitable with paperless accounts payable
COMMON:  Join us at the Annual 2008 conference, March 30 - April 3, in Nashville, Tennessee
Help/Systems:  SEQUEL is the single solution for all your business intelligence needs


IT Jungle Store Top Book Picks

The System i Pocket RPG & RPG IV Guide: List Price, $69.95
The iSeries Pocket Database Guide: List Price, $59.00
The iSeries Pocket Developers' Guide: List Price, $59.00
The iSeries Pocket SQL Guide: List Price, $59.00
The iSeries Pocket Query Guide: List Price, $49.00
The iSeries Pocket WebFacing Primer: List Price, $39.00
Migrating to WebSphere Express for iSeries: List Price, $49.00
iSeries Express Web Implementer's Guide: List Price, $59.00
Getting Started with WebSphere Development Studio for iSeries: List Price, $79.95
Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
Getting Started with WebSphere Express for iSeries: List Price, $49.00
WebFacing Application Design and Development Guide: List Price, $55.00
Can the AS/400 Survive IBM?: List Price, $49.00
The All-Everything Machine: List Price, $29.95
Chip Wars: List Price, $29.95

 

The Four Hundred
IBM Focusing on i5 Account Sales, Not i5 Sales

Dr. Frank Soltis at COMMON: A Show Worth Watching

i5/OS Curriculum Contingent on Job Prospects, Business Community

As I See It: Education--the Other Dysfunction

The Linux Beacon
Brazilian Game Site Chooses Hybrid Mainframe-Cell Platform

Q&A with HP's Paul Miller: The X64 Server Biz

How To Build a Green Data Center

As I See It: Induced Labor

Four Hundred Stuff
Arcad Positions for Growth in Change Management

Profound Releases Genie, Lauded for Disney Work

iMessaging Adopts SIP for Call Center Software

ABL Unveils Strategi SOA

Big Iron
Brazilian Game Site Chooses Hybrid Mainframe-Cell Platform

Top Mainframe Stories From Around the Web

Chats, Webinars, Seminars, Shows, and Other Happenings

System i PTF Guide
May 5, 2007: Volume 9, Number 18

April 28, 2007: Volume 9, Number 17

April 21, 2007: Volume 9, Number 16

April 14, 2007: Volume 9, Number 15

April 7, 2007: Volume 9, Number 14

March 31, 2007: Volume 9, Number 13

The Windows Observer
Microsoft Releases First Public Beta of 'Longhorn' Server

How To Build a Green Data Center

Strong Office 2007 Sales Push Microsoft to Record Profit

Startup 3Leaf Systems Looks to Shake Up Server Virtualization

The Unix Guardian
More Details Emerge on IBM's Upcoming Power6 Server Launch

Sun Boots Solaris 10 on "Rock" Sparc Processors

Startup 3Leaf Systems Looks to Shake Up Server Virtualization

Mad Dog 21/21: Hearts and Minds

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

THIS ISSUE SPONSORED BY:

ProData Computer Services
WorksRight Software
Guild Companies



TABLE OF CONTENTS
WHERE Versus HAVING

Error-Checking Email Addresses, for Intelligent People

Admin Alert: The i5 Battery Checking Process

Four Hundred Guru

BACK ISSUES

From the IT Jungle Forums
Position: iSeries System Engineer

Encoded Vector Index (EVI)--throw some light

User profile for webserver instances

Can a program be written to pull data from the network?

Command line question





 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement