INF: Mix of Aggregate Functions & Column Names w/out GROUP BY

Last reviewed: April 28, 1997
Article ID: Q80284

The information in this article applies to:

  - Microsoft SQL Server version 4.2 for OS/2
  - Microsoft SQL Server version 4.2

SUMMARY

When both aggregate functions and table columns are included in the select list of a query without the GROUP BY clause, the WHERE clause applies only to the rows included in the calculation of the aggregate function, but does not restrict the rows returned by the query. Similarly, if a HAVING clause is used without a GROUP BY clause (which is proper in SQL Server), a HAVING clause restricts the rows returned by the query but does not affect the calculation of the aggregate function.

MORE INFORMATION

Consider the following query

   select count (stor_id), stor_id from stores WHERE stor_id="7066"

which returns the following results:

          stor_id
   ------ -------
       1  7066
       1  7067
       1  7131
       1  8042
       1  6380
       1  7896

Note that the condition in the WHERE clause does not restrict the rows returned.

Similarly, the following query

   select count (stor_id), stor_id from stores HAVING stor_id="7066"

returns the following results:

          stor_id
   ------ -------
       6  7066

Note also that the condition in the HAVING clause does not affect the calculation of the aggregate function.

If both WHERE and HAVING are used, then both the returned rows and the calculation of the aggregate function are restricted. The following query shows this clearly:

   select count (stor_id), stor_id from stores WHERE stor_id="7066"
                                               HAVING stor_id="7066"

This query returns:

          stor_id
   ------ -------
       1  7066

The above properties of the WHERE and HAVING clauses also apply to all other types of aggregate functions.

Although it is generally considered meaningless to mix column names with aggregate functions in the select list without GROUP BY, it is used in practice anyway. Therefore, special caution should be taken when using these types of queries because users can easily misinterpret their meaning and get "unexpected" query results.


Additional query words: Windows NT dblib
Keywords : kbprg SSrvDB_Lib SSrvGen SSrvTrans SSrvWinNT
Version : 4.2 | 4.2
Platform : OS/2 WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: April 28, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.