Clauses in Oracle
Clauses in Oracle
Sub Total and Grand Total Based On the Given Values Automatically.
> Rollup - Finding Sub Total & Grand Total (Single Column)
> Cube - Finding Sub Total & Grand Total (Multiple Columns)
Syntax:
Where Clause:
> Filtering Rows in One By One Manner before Grouping Data in Table.
Syntax:
Ex:
Note: "Where" Clause Can Be Used In "Select " ,"Update" And "Delete" Commands
Only.
Order by Clause:
Syntax:
Select * / <List Of Column Names> From <Tn> Order By <Column Name1> <Asc /
Desc>,<Column Name2> <Asc/Desc>,............;
Ex1:
Sol:
(Or)
Ex2:
Sol:
Ex3:
Descending Order?
Sol:
Sol:
Note:
Ex:
Note:
Using Order by Clause On "Null" Values Column Then Oracle Returns "Null" Values
Last In Ascending Order And "Null" Values Are Displayed First In Descending By
Default.
If We Want To Change This Default Order Of "Null" Then We Use Null Clauses
Are “Nulls First" And " Nulls Last ".
Ex:
Group By:
> When We Use "Group by "We Must Use "Aggregative Functions" Are
Sum(),Avg(),Min(),Max(),Count().
Syntax:
Group By
Aggregative Functions. |
(4) (4)
Ex1:
Sol:
Ex2:
Sol:
Deptno Wise ?
Sol:
Group By Job,Deptno;
Ex4:
Sol:
Ex5:
Sol:
Having:
> Filtering Rows after Grouping Data in Table. It Can Be Used Along With "Group
By" Clause.
Syntax:
Ex1:
Waq To Find Out No.Of Employee Of Each Job In Which Job No.Of Employee
Sol:
Having Count(*)>3;
Ex2:
Waq To Display Sum Of Salary Of Deptno's From Emp Table.If Sum Of Salary
Sol:
Where Having
------------ --------------
A Table. Of A Table.
Syntax:
Group By <Col1>,<Col2>,......................
];
Ex:
Where Sal>1000
Group By Deptno
Having Count(*)>3
Order By Deptno;
Deptno Count(*)
------ ----------
20 4
30 5
Order of Execution:
> From
> Where
> Group By
> Having
> Select
> Order By
> Rollup Will Find Sub & Grand Total Based On a Single Column.
> Cube Will Find Sub & Grand Total Based On Multiple Columns.
Syntax:
Deptno Count(*)
---------- ----------
10 3
20 5
30 6
14
Sql> Select Deptno, Job, Count (*) From Emp Group By Rollup(Deptno,Job);
Note: In The Above Ex. Rollup Is Finding Sub & Grand Total Based On A Single
Column (Deptno).If We Want To Find Sub & Grand Total Then Use "Cube" Clause.
Syntax:
Sql> Select Deptno, Count (*) From Emp Group By Cube(Deptno) Order By
Deptno;
Sql> Select Deptno, Job, Count (*) From Emp Group By Cube(Deptno , Job)
Order by Deptno;
Grouping_ID ():
> It Used More Compact Way To Identify Sub And Grand Total Rows.
Grouping_ID (<Col1>,<Col2>,.....)
Ex:
Select Deptno, Job, Count (*), Grouping_ID (Deptno, Job) From Emp