Home » SQL & PL/SQL » SQL & PL/SQL » Group by and Order by
Group by and Order by [message #2066] Wed, 19 June 2002 14:44 Go to next message
ling
Messages: 2
Registered: June 2002
Junior Member
Hi. I have question about group by and order by statement.

If I have a query using group by statement, do I still need an order by statement?

select col1, col2, col3, sum(col4)
from table1
group by col1, col2
order by col1, col2 (-- Is this statment necessary?)

Thank,

Ling
Re: Group by and Order by [message #2067 is a reply to message #2066] Wed, 19 June 2002 15:51 Go to previous messageGo to next message
Siva Ram
Messages: 22
Registered: November 2001
Junior Member
Hi Ling

It depends on your requirement. If U need the tuples to be displayed in ascending order of col1 and then by col2 ORDER BY clause is required.

Thank You
Siva Ram
Re: Group by and Order by [message #2069 is a reply to message #2066] Wed, 19 June 2002 16:08 Go to previous messageGo to next message
Amit Chauhan
Messages: 74
Registered: July 1999
Member
Hi,
By default, in a group by statement the order is ascending. So you can get away with order by if you are happy with the default order. If you want the order to be something different, you will have to use order by clause.
e.g
select a, count(*)
from xyz
group by a

will give the result with a ordered ascending.
So the above statement is equivalent to :
select a, count(*)
from xyz
group by a
order by a asc

If you want the reverse order you will have to give the order by clause :
select a, count(*)
from xyz
group by a
order by a desc

Hope that helps
Amit
Re: Group by and Order by [message #2085 is a reply to message #2066] Thu, 20 June 2002 06:01 Go to previous messageGo to next message
koteswara rao p.
Messages: 10
Registered: June 2002
Junior Member
hi
Ling

when we use group by statement it automatically
sorts the result in assending order of the columns.
if you need the result in descending order or you
want to sort the result in order by c2,c1 then
the order by is necessary.

have a nice day

koti
Re: Group by and Order by [message #2088 is a reply to message #2066] Thu, 20 June 2002 06:32 Go to previous message
ling
Messages: 2
Registered: June 2002
Junior Member
Hi.

Thank you all for the help.

Ling
Previous Topic: SQL QUERY
Next Topic: How to deal with ORA-04020 under Oracle 8i ?
Goto Forum:
  


Current Time: Fri Apr 26 03:16:29 CDT 2024