0% found this document useful (0 votes)
0 views

Oracle_5

Uploaded by

mohammed.shohel9
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Oracle_5

Uploaded by

mohammed.shohel9
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Aggregate functions:

Aggregate functions returns a single row output based on group of rows.


These functions can appear in select list and having clauses only.
The functions operate on set of rows to give one result per each group.

i) avg:
It returns the average value of the column, It ignores the null values

Ex: select Avg (sal) from emp;

ii) sum:
It returns the sum value (total value) of a column

Ex: select sum (sal) from emp;

iii) max:
It returns the maximum value of the column

Ex: select Max(sal) from emp;

iv) min:
It returns the minimum value of the column

Ex: select min(sal) from emp;

v)std dev (standard deviation):


It returns the standard deviation of the column

Ex: select Stddev(sal) from emp;

vi) variance:
It returns the variance of the column

Ex: select variance(sal) from emp;

vii) count:
It returns the count of total number of rows in column or table.
If * or any number is used it will return all rows including null values.
If we specify the column name it counts the distinct values and eliminates null values

Ex: select count(*), count(mgr) from emp;

Viii) distinct:
It returns the distinct rows of a columns, it eliminates the duplicate rows

Ex: select distinct(sal) from emp;


ix ) Greatest:

It returns the greatest of the value in the list of expression.


It works on particular rows

Syn: greatest (col1,col2…..coln)

Ex: if we have 5 students in a table and 6 different subjects, for every student if you want to find out in
which subject student has gotten the highest marks on this case we can use greatest function

S_name S1 S2 S3 S4 S5 S6
Prakash 84 86 88 75 68 82
Alex 75 87 90 95 90 80
Martin 96 97 95 94 93 92
Denis 90 89 95 92 91 97
Den mort 85 88 90 97 98 80

Select s_name, greatest (s1, s2, s3, s4, s5, s6) from new;

Prakash 88
Alex 95
Martin 97
Denis 97
Den mort 98

x) Least:

It returns the least value in the list of expressions

Ex: select s_name, least (s1, s2, s3, s4, s5, s6) from new;

Prakash 68
Alex 75
Martin 92
Denis 89
Den mort 80
CLAUSES IN ORACLE

Clauses are classified in to four types.

1) Where clause:
It is used to limit the records.
By providing conditions we can limit the records.
Where clause doesn’t allow group by function in where condition.

Ex: select * from emp where sal >1000;


select * from emp where deptno = 20 and hiredate >’01-JUN-81’;

2) Groupby clause:

The group by clause is used to collect data from multiple records to group the results that have
matching values from one or more columns.

Group by clause is normally used when we have aggregate function in select clause.
Aggregate functions are like Min, Max, Count, Sum, Avgetc

Syn: Select col1, col2,….coln, aggregate function (col ) from table-name where condition group by col1,
col2,….coln;

Ex: select deptno, sum(sal) from emp where sal > 500 group by deptno;

Note: where condition is optional to filter the records, the where clause should be preceded by group by
clause.
“All the columns used in select statement must be included in the group by clause ”.

3) Having clause:

It is used to filter the rows returned by the group by clause.


We cannot use having clause without group by clause.

Syn: select col1, col2,…coln from table-name where condition group by col1, col2,……coln having
condition.

Ex: select deptno, max(sal) from emp where sal> 2000 group by deptno having max(sal) >3000;
4) Order by clause:

The order by clause is used to sort the records/rows in ascending or descending order.
By default it sorts in ascending order.

Syn: select column1, column2… column from table_name order by ( asc/desc );

Note: If we omit ascending or descending from the syntax then default data will be sorted in ascending
order.

Ex: select ename, sal from emp where sal> 2000 order by empno, sal;

Here when we are specifying the column names in order by clause, instead of columns we can give
1,2,3,4,… numbers to sort the data.

Ex1: select empname, deptno, sal from emp order by 2;


Here order by 2 specifies that 2nd column mentioned in the select list i.edeptno

Ex2: select empno, sal, mgr from emp order by sal desc;

ANALYTICAL FUNCTIONS
The analytical functions in oracle help us to analyze the data.

A) Rollup:
The rollup function is used to calculate multiple levels of subtotals across the specified group
Of dimensions.
It also calculates the grand total .It is the sample extension to group by clause.

EX: select job,count(*)from emp group by rollup(job);

In the above example the rollup function calculates the grand total of all count of jobs.

EX: select deptno, job, count(*) from emp group by rollup (job,deptno);

In the above example the rollup function counted the total of all multiple sublevels of managers and
grand total.

B) Cube:

The cube function is used to calculate subtotals for all possible combinations of group dimensions.
EX:- select deptno, job, count(*) from emp group by cube (job, deptno);

C) Rank:
The rank function returns the rank of a value in group of values.
Note: The rank function gives the non-consecutive ranking if the tested values are same.

The rank function can be used be in two ways.

1) Aggregate Function.
2) Analytical Function.

1) Rank as Aggregate function :

The rank function returns the rank of a row within group of rows.

EX: select rank (1000) within group (order by sal) from emp;

2) Rank as analytical function:


Rank as analytical function returns the rank of the row by dividing other groups.

Syntax: rank () over ( order by clause)


Ex: select ename, rank() over ( order by sal desc ) from emp;

Rank with partition by clause:

EX:- select empno, ename, deptno, sal, rank() over (partition by deptno order by sal desc) from emp;

In the above example ranks has been given on salaries on department wise.

If we see the ranking column the rank function has given the non-consecutive rankings such as for
deptno 20 it has given as 1 1 3 and it skipped the second rank.

Note: to get the consecutive rankings we have to use the dense_rank function .

D) dense_rank:

It is also used to assign the ranks. With the help of dense_rank we can make sure that ranks don’t get
skipped for common values.

Syntax: dense_rank () over ( order by clause)


Ex: select ename, dense_rank() over ( order by sal desc ) from emp;

You might also like