You are on page 1of 12

# Total Online Users: 1896

## Home | Forums | Videos | Blogs Login

E nter U s erI D or E mail

Search :

## Aggregate Function in Oracle

By Sapna Malik November 02, 2010

## Similar Articles Author's other articles Sponsored by

Aggregate Function
Similar Articles

The aggregate functions return a SUM() Function in Oracle SQL Plus single value based on What is Aggregate Function ? groups of rows, rather than single How to use Maximun Aggregate operator in LINQ Partvalue for each row. 1 The aggregate Aggregate Functions in SQL : Part 2 functions ignore null Aggregating Data using Group Functions in Oracle values. We can use Aggregate functions More... in select lists and in ORDER BY and Sponsored by HAVING clauses. Become a Sponsor They are commonly used with the GROUP BY clause in a SELECT statement, where Oracle divides the rows of a queried table or view into groups. All Aggregate Function: 1. 2. 3. 4. 5. 6. 7. 8. AVG Function CORR Function COUNT Function COVAR_POP Function COVER_SAMP Function GROUPING Function MAX Function MIN Function

## 9. 10. 11. 12. 13. 14. 15. 16.

REGR_(LINEAR REGRESSION) Functions STDDEV Function STDDEV_POP Function STDDEV_SAMP Function SUM Function VAR_POP Function VAR_SAMP Function VARIANCE Function

1. AVG Function: The AVG() function returns average value of expression. We can use it as an aggregate or analytic function. Syntax: SELECT AVG ("Expression") FROM "Table _Name"; Example: The following example calculates the average salary of all employees in the EMP5 table. SQL> SELECT AVG ("SALARY") 2 FROM EMP5; Output:

2. CORR Function: The CORR() function returns the coefficient of correlation of a set of number pairs. We can use it as an aggregate or analytic function. Both expression1 and expression2 are number expressions. Oracle applies the function to the set of (expression1 , expression2) after eliminating the pairs for which either expression1 or expression2 is null. Syntax: CORR( expression1 ,expression2) [ over (analytic_clause) ] Example: SQL> SELECT CORR(SALARY,EMP_ID) 2 FROM EMP5; Output:

3. COUNT Function: The COUNT() function returns the number of rows in the query. We can use it as an aggregate or analytic function. If we specify expression, COUNT returns the number of rows where expression is not null. We can count either all rows, or only distinct values of expression. If we specify the asterisk (*), this function returns all rows, including duplicates and nulls. COUNT never returns null. Syntax: SELECT COUNT(*) FROM Table_Name; Example: SQL> SELECT COUNT(*) "Total Row" 2 FROM EMP5; Output:

4. COVAR_POP() fUNCTION: The COVAR_POP() function returns the population covariance of a set of number pairs. We can use it as an aggregate or analytic function. The cover_pop function as a corr function. Syntax: COVAR_POP(expression1, expression2 ) Both expression1 and expression2 are number expressions. Oracle applies the function to the set of (expression1 , expression2) after eliminating the pairs for which either expression1 or expression2 is null.

## Example: SQL> SELECT COVAR_POP(SALARY,EMP_ID) 2 FROM EMP5; Output:

5. COVER_SAMP Function: The COVER_SAMP() function returns the sample covariance of a set of number pairs. We can use it as an aggregate or analytic function. Syntax: COVAR_SAMP(expression1, expression2 ) Both expression1 and expression2 are number expressions. Oracle applies the function to the set of (expression1 , expression2) after eliminating the pairs for which either expression1 or expression2 is null. Example: SQL> SELECT COVAR_SAMP(SALARY,EMP_ID) 2 FROM EMP5; Output:

6. GROUPING Function: The GROUPING() function is applicable only in a select statement that contains a GROUP BY extension, such as ROLLUP or CUBE. These operations produce superaggregate rows that contain nulls representing the set of all values. You can use the GROUPING function to distinguish a null that represents the set of all values in a superaggregate

row from an actual null. Example: SQL> SELECT GROUPING(EMP_NAME) 2 FROM EMP5 3 GROUP BY "EMP_NAME"; Output:

7. MAX Function: The MAX()function returns maximum value of expression. We can use it as an aggregate or analytic function. Example: This example returns the highest salary. SQL> SELECT MAX(SALARY) 2 FROM EMP5; Output:

8. MIN Function: The MIN() function returns minimum value of expression. We can use it as an aggregate or analytic function. Example: This example returns the lowest salary. SQL> SELECT MIN(SALARY) 2 FROM EMP5; Output:

9. REGR_(LINEAR REGRESSION) Functions: The linear regression functions fit an ordinary-least-squares regression line to a set of number pairs. We can use them as both aggregate and analytic functions. The linear regression functions are: REGR_SLOPE REGR_INTERCEPT REGR_COUNT REGR_R2 REGR_AVGX REGR_AVGY REGR_SXX REGR_SYY REGR_SXY Oracle applies the function to the set of (expression1, expression2) pairs after eliminating all pairs for which either expression1 or expression2 is null. Oracle computes all the regression functions simultaneously during a single pass through the data. expression1 is interpreted as a value of the dependent variable (a "y value"), and expression2 is interpreted as a value of the independent variable (an "x value"). REGR_SLOPE() Function: The REGR_SLOPE()function returns the slope of the line. Example: SQL> SELECT REGR_SLOPE(SALARY,EMP_ID) 2 FROM EMP5; Output:

REGR_INTERCEPT Function: The REGR_INTERCEPT()function returns the y-intercept of the regression line. SQL> SELECT REGR_INTERCEPT(SALARY,EMP_ID) 2 FROM EMP5; REGR_COUNT Function: The REGR_COUNT()function returns an integer that is the number of non-null number pairs used to fit the regression line. SQL> SELECT REGR_COUNT(SALARY,EMP_ID) 2 FROM EMP5; REGR_R2 Function: The REGR_R2()function returns the coefficient of determination (also called "R-squared" or "goodness of fit") for the regression. SQL> SELECT REGR_R2(SALARY,EMP_ID) 2 FROM EMP5; REGR_AVGX Function: The REGR_AVGX()function evaluates the average of the independent variable (express12) of the regression line. SQL> SELECT REGR_AVGX(SALARY,EMP_ID) 2 FROM EMP5; REGR_AVGY Function: The REGR_AVGY()function evaluates the average of the dependent variable (express1) of the regression line. SQL> SELECT REGR_AVGY(SALARY,EMP_ID) 2 FROM EMP5; The REGR_SXY(), REGR_SXX(), REGR_SYY() function are auxiliary functions that are used to compute various diagnostic statistics. Example: REGR_SXX Function: SQL> SELECT REGR_SXX(SALARY,EMP_ID) 2 FROM EMP5; REGR_SYY Function: SQL> SELECT REGR_SYY(SALARY,EMP_ID) 2 FROM EMP5; REGR_SXY Function: SQL> SELECT REGR_SXY(SALARY,EMP_ID) 2 FROM EMP5; 10. STDDEV Function: The STDDEV() function returns sample standard deviation of expression, a set of numbers. We can use it as both an aggregate and analytic function. Oracle calculates the standard deviation as the square root of the variance defined for the VARIANCE aggregate function. Example:

## SQL> SELECT STDDEV("SALARY") 2 FROM EMP5; Output:

11. STDDEV_POP Function: The STDDEV_POP() function computes the population standard deviation and returns the square root of the population variance Example: The following example returns the population and sample standard deviations of profit from salary in the EMP5 table. SQL> SELECT STDDEV_POP(SALARY) 2 FROM EMP5; Output:

12. STDDEV_SAMP Function: The STDDEV_SAMP() function computes the cumulative sample standard deviation and returns the square root of the sample variance. Example: SQL> SELECT STDDEV_SAMP(SALARY) 2 FROM EMP5; Output:

13. SUM Function: The SUM function returns the summed value of an expression. The expression can be a numeric field and formula. Syntax: SELECT SUM ("Expression") FROM "Table _Name"; Example: SQL> SELECT SUM ("SALARY") 2 FROM EMP5; Output:

14. VAR_POP Function: The VAR_POP() function returns the population variance of a set of numbers after discarding the nulls in this set. Example: SQL> SELECT VAR_POP(SALARY) 2 FROM EMP5; Output:

15. VAR_SAMP Function: The VAR_SAMP() function returns the sample variance of a set of numbers after discarding the nulls in this set Example: SQL> SELECT VAR_SAMP(SALARY) 2 FROM EMP5; Output:

16. VARIANCE Function: The VARIANCE() function returns variance of expression. We can use it as an aggregate or analytic function. Oracle calculates the variance of expression as follows: 0 if the number of rows in expression = 1 VAR_SAMP if the number of rows in expression > 1 Example: The following example calculates the variance of all salaries in the EMP5 table. SQL> SELECT VARIANCE(SALARY) 2 FROM EMP5; Output:

Comment Request!

Sapna Malik

Subject: Comment: