Oracle_5
Oracle_5
i) avg:
It returns the average value of the column, It ignores the null values
ii) sum:
It returns the sum value (total value) of a column
iii) max:
It returns the maximum value of the column
iv) min:
It returns the minimum value of the column
vi) variance:
It returns the variance of the column
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
Viii) distinct:
It returns the distinct rows of a columns, it eliminates the duplicate rows
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:
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
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.
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:
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.
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.
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.
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.
1) Aggregate Function.
2) Analytical 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;
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.