Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

 
 Home
 E-mail Us
 Oracle Articles
New Oracle Articles


 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog


 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Analysis
 Design
 Implementation
 Oracle Support


 SQL Tuning
 Security

 Oracle UNIX
 Oracle Linux
 Monitoring
 Remote s
upport
 Remote plans
 Remote
services
 Application Server

 Applications
 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S
upport  
 Development  

 Implementation


 Consulting Staff
 Consulting Prices
 Help Wanted!

 


 Oracle Posters
 Oracle Books

 Oracle Scripts
 Ion
 Excel-DB  

Don Burleson Blog 


 

 

 


 

 

 

 
 

Advanced Oracle SQL: Standard Aggregate Functions

Oracle Tips by Laurent Schneider

 

Laurent Schneider is considered one of the top Oracle SQL experts, and he is the author of the book "Programming" by Rampant TechPress.  The following is an excerpt from the book.

An aggregate function in SQL is a function that returns a single value from multiple rows.

Standard Aggregate Functions

An aggregate function can be used over the whole table to return a single value:

SELECT
   COUNT(ENAME)
FROM
   EMP;
COUNT(ENAME)
------------
          14

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     1 |     6 |     3   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE    |      |     1 |     6 |            |          |
|   2 |   TABLE ACCESS FULL| EMP  |    14 |    84 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------
 

The number of employees in the EMP table is returned.  

SELECT
   DEPTNO,
   SUM(SAL)
FROM
   EMP
GROUP BY
   DEPTNO;

    DEPTNO   SUM(SAL)
---------- ----------
        30       9400
        20      10875
        10       8750

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     3 |    21 |     4  (25)| 00:00:01 |
|   1 |  HASH GROUP BY     |      |     3 |    21 |     4  (25)| 00:00:01 |
|   2 |   TABLE ACCESS FULL| EMP  |    14 |    98 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------

The group is the department. Note the operation HASH GROUP BY in the execution plan. This operation appeared in 10gR2 and requires no sorting.

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     3 |    21 |     4  (25)| 00:00:01 |
|   1 |  SORT GROUP BY     |      |     3 |    21 |     4  (25)| 00:00:01 |
|   2 |   TABLE ACCESS FULL| EMP  |    14 |    98 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------

In earlier releases, the operation to group the rows was a SORT GROUP BY operation and it implied sorting. This optimization affected applications that rely on implicit sorting. In 10gR2 and later, sorting is achieved by using the ORDER BY clause.

Multiple aggregate functions can be used in the same query. For example, the maximum and the minimum salary can be returned as a single row:

SELECT
   MIN(SAL),
   MAX(SAL)
FROM
   EMP;
  MIN(SAL)   MAX(SAL)
---------- ----------
       800       5000

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     1 |     4 |     3   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE    |      |     1 |     4 |            |          |
|   2 |   TABLE ACCESS FULL| EMP  |    14 |    56 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------

Both values are returned. The function AVG computes the average:

SELECT
   COUNT(*),
   COUNT(COMM),
   SUM(COMM),
   AVG(COMM)
FROM
   EMP;

  COUNT(*) COUNT(COMM)  SUM(COMM)  AVG(COMM)
---------- ----------- ---------- ----------
        14           4       2200        550

Notice the null values are ignored; only the not null values are taken to evaluate the average. Null values are never aggregated. 

Note:  COUNT(*) is the best way to count the rows; it is wrong to assume anything else like COUNT(1) will perform faster.

COUNT(*) is a special syntax that instructs Oracle to count all rows including nulls.

STATS_MODE is a very efficient function which returns the mode value for a column, i.e. the value that appears most frequently.

SELECT
   STATS_MODE(DEPTNO)
FROM
   EMP;
STATS_MODE(DEPTNO)
------------------
                30

The department with the most employees is returned. The function is not deterministic. In case of a tie, the chosen modal value may differ from one execution to another.


 

 

��  
 
 
Oracle Training at Sea
 
 
 
 
oracle dba poster
 

 
Follow us on Twitter 
 
Oracle performance tuning software 
 
Oracle Linux poster
 
 
 

 

Burleson is the American Team

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  

and include the URL for the page.


                    









Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


 

Copyright © 1996 -  2020

All rights reserved by Burleson

Oracle ® is the registered trademark of Oracle Corporation.