Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

5/8/2015

GROUPBYClauseinOracleSQLPLSQLTechHoney

TECHHONEY
OAFramework

The#1Webs itefor O r ac lePL/SQ L,O AFr amew or k andH TML

DataBase

TechHoneyclauseGROUPBYClauseinOracleSQLPLSQL
LIKEConditioninOracleSQLPLSQL

HAVINGClauseinOracleSQLPLSQL

Categories
Attributes

GROUPBYClauseinOracleSQLPLSQL

clause

TheGROUPBYclauseinOracleSQL/PLSQLisusedinSELECTstatementandallowsustofiltertherecords
bygroupingthemasperoneormorecolumns.

condition
function
HTML
Items
loop
OAFMiscellaneous
Oracle
PLSQLHowTo

SyntaxfortheGROUPBYclauseinOracleSQL/PLSQLis:

plsql

SELECTcolumn_name1

Regions

,column_name2

sql

,column_name3

statement

Tags

RecentComments

,column_nameN
Aggregate_function(value/expression)

techhoneyonFOREIGNKEYwithONDELETE

FROMtable_name

CASCADEinOracleSQLPLSQL

WHEREconditions

VaradSarveonFOREIGNKEYwithONDELETE

CASCADEinOracleSQLPLSQL

GROUPBYcolumn_name1
,column_name2

techhoneyonMessageLOVInputinOAF

badrionMessageLOVInputinOAF

techhoneyonFOREIGNKEYwithONDELETE

CASCADEinOracleSQLPLSQL

column_nameN
TheaggregatefunctioncanbeSUM(),MIN(),MAX()orCOUNT().
Letstakeanexampleforunderstanding:

Supposehaveatablenamedemployeeinthedatabaseasshownbelow.
Employee_ID

Employee_Name

Salary

Department

Commission

101

EmpA

10000

Sales

10

102

EmpB

20000

IT

20

103

EmpC

28000

IT

20

104

EmpD

30000

Support

105

EmpE

32000

Sales

10

Scenario1:
SupposewewanttoseehowmuchSalaryisbeinggiventowhichdepartment.

http://www.techhoney.com/oracle/clause/groupbyclauseinoraclesqlplsql/

1/3

5/8/2015

GROUPBYClauseinOracleSQLPLSQLTechHoney

Wecanachievethesameas:

SELECTdepartment
,SUM(Salary)Total_Salary
FROMemployee
GROUPBYdepartment

Theresultoftheabovequerywillbe:
Department

Total_Salary

Support

30000

IT

48000

Sales

42000

Herewehaveretrievedtherecordsexplainingusthat30000intotalisbeinggiventoemployee(s)workinginthe
Salesdepartment,48000intotalisbeinggiventoemployee(s)workingintheITdepartmentand42000isbeing
giventoemployee(s)workingintheSalesdepartmentusingGROUPBYclauseinSELECTstatement.
Also,notethatTotal_SalaryaftertheaggregatefunctionSUM(salary)actsasanaliasnameforthecolumnin
queryresult.

Scenario2:
SupposewewanttoseethemaximumSalarybeinggivenineverydepartment
Letsassumethatwehaveanewtablenamelyemployeeasshownbelow:
Employee_ID

Employee_Name

Salary

Department

Commission

101

EmpA

10000

Sales

10

102

EmpB

20000

IT

20

103

EmpC

28000

IT

20

104

EmpD

30000

Support

105

EmpE

32000

Sales

10

Wecanachievethesameas:

SELECTdepartment
,MAX(salary)Maximum_Salary
FROMemployee
GROUPBYdepartment

Oncewehaveruntheabovecodefollowingwillbetheresult:
Department

Total_Salary

Support

30000

IT

28000

Sales

32000

Herewehavesuccessfullyretrievedtherecordspertainingtomaximumsalarybeinggivenineachdepartment
usingGROUPBYclauseinSELECTstatement.

http://www.techhoney.com/oracle/clause/groupbyclauseinoraclesqlplsql/

2/3

5/8/2015

GROUPBYClauseinOracleSQLPLSQLTechHoney

Bookmarkthepermalink.

LIKEConditioninOracleSQLPLSQL

HAVINGClauseinOracleSQLPLSQL

Pingback:Oracle/SQLMINFunction|Syntax/ExamplesTechHoney.com
Pingback:Oracle/SQLSUMFunction|Syntax/ExamplesTechHoney.com
Pingback:Oracle/SQLAverage/AVGFunction|Syntax/ExamplesTechHoney.com

DisclaimerSiteMap

TechHoney|PoweredbyMantra&WordPress.

http://www.techhoney.com/oracle/clause/groupbyclauseinoraclesqlplsql/

3/3

You might also like