IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Getting confusion about Groupby and Orderby


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
praneethgokul

New User


Joined: 06 May 2005
Posts: 20
Location: Bangalore

PostPosted: Mon May 16, 2005 10:10 am
Reply with quote

hi all,

Everytime I am getting confusion about Groupby and Orderby.

Can anybody please clarify me, with an example.


Thanks in advance.

Praneeth
Back to top
View user's profile Send private message
manikant pathak

New User


Joined: 09 May 2005
Posts: 37
Location: bangalore

PostPosted: Mon May 16, 2005 10:57 am
Reply with quote

hi,

the diff between group by and order by can be better understood by the following eg:

query 1: select * from table-name
group by column-name;

query 2: select * from table-name
order by column-name;

suppose the column-name in the above eg is dept ......then in the first query all the employee belonging to same department will be displayed together with dept name being displayed once.

but in the latter case the default order is ASC(ascending)...and the result set will be the details of the employee with dept in ascending order and if more than one employees r with the same dept that dept will be repeated for each employee.



i hope this will clarify ur confusion....
Back to top
View user's profile Send private message
ovreddy

Active User


Joined: 06 Dec 2004
Posts: 211
Location: Keane Inc., Minneapolis USA.

PostPosted: Mon May 16, 2005 12:26 pm
Reply with quote

Hi Praneeth,

ORDER BY: It is used to arrange the rows in an Ascending or descending order of a column.

SELECT * FROM EMP ORDER BY SAL;
(Will display the rows in ascending order of salary)
SELECT * FROM EMP ORDER BY SAL;

GROUP BY: This is used to group a set of rows then apply a group function like MAX,MIN,AVG,SUM on it. It gives group wise details.

SELECT DEPTNO,COUNT(*) FROM EMP GROUP BY DEPTNO;
(displays dept wise number of employees)


Thanks,
Reddy.
Back to top
View user's profile Send private message
notonly4u

New User


Joined: 26 Apr 2005
Posts: 87
Location: Hyderabad

PostPosted: Mon May 16, 2005 3:46 pm
Reply with quote

One more thing to add....

If we are using Group by we can't use Where condition but we can use Having clause.

Eg: select * from emp group by dept having sal > 2000;

If Iam wrong please make a note.

Regards
Tanden
Back to top
View user's profile Send private message
ovreddy

Active User


Joined: 06 Dec 2004
Posts: 211
Location: Keane Inc., Minneapolis USA.

PostPosted: Mon May 16, 2005 5:05 pm
Reply with quote

Hi Tanden,

Sorry! We can use WHERE and HAVING together. I want to clear it to you. WHERE clause will check condition for each row but HAVING will check condition for a group of rows.

WHERE & GROUP BY:

SELECT DEPTNO,COUNT(*) FROM EMP WHERE SAL>1000 GROUP BY DEPTNO;
(displays dept wise number of employees whose salary is greater than 1000)

HAVING: Group by is used to sub group the rows based on same column values like DEPTNO wise group. Then in that group we need to select a group based on condition in such a case we need to use HAVING.

HAVING is used to check conditions with GROUP functions like MAX,MIN,SUM etc...

SELECT DEPTNO,COUNT(*) FROM EMP GROUP BY DEPTNO HAVING COUNT(*) >=3;
(It lists dept wise number of employees if the dept has atleast 3 employees)

I Think now its clear to you. If you have more ques please get back to me.

Thanks,
Reddy.
Back to top
View user's profile Send private message
praneethgokul

New User


Joined: 06 May 2005
Posts: 20
Location: Bangalore

PostPosted: Tue May 17, 2005 6:51 pm
Reply with quote

ThanQ manikanth
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Confusion regarding length of SQLCA v... DB2 4
No new posts Confusion b/w index and subscript Mainframe Interview Questions 7
No new posts DFSORT : GroupBy DFSORT/ICETOOL 1
No new posts Confusion while passing data to a cal... COBOL Programming 5
No new posts Joinkeys to Groupby and sum up DFSORT/ICETOOL 3
Search our Forums:

Back to Top