Can GROUP BY and ORDERED BY used in a single query?

Questions by Alok.Ray

Showing Answers 1 - 21 of 21 Answers

Vinod Maanju

  • Dec 30th, 2005
 

YES we can use and Group by must come in order to code query.Ex. select * from tab01 group by col1 order by col02

  Was this answer useful?  Yes

sai

  • Jan 31st, 2006
 

We can use Group By and order by in a single query only if both of them(Group by and order by)  use the same column.

  Was this answer useful?  Yes

Culver_lake

  • Mar 18th, 2006
 

GROUP  BY and ORDER BY can be used in the same query and it is NOT required that they be the same column.  GROUP BY controls the way the data is organized for sumarization.  ORDER BY simply sorts the rows of the result.  The following is legal:

SELECT  DEPARTMENT, AVG(SALARY) AS AVG

FROM     EMPLOYEE

GROUP BY DEPARTMENT

ORDER BY AVG

  Was this answer useful?  Yes

Mahesh

  • Jun 22nd, 2006
 

Group by controls the presentation of the rows, 

Order by controls the presentation of the columns  for the results of the SELECT statement

  Was this answer useful?  Yes

Varsha Das

  • May 27th, 2015
 

This is generating an error message as follows: "An expression starting with "col2" specified in a SELECT clause, HAVING clause, or ORDER BY clause is not specified in the GROUP BY clause or it is in a SELECT clause, HAVING clause, or ORDER BY clause with a column function and no GROUP BY clause is specified." The query being

"SELECT col1, col2, col3
FROM table1
WHERE col1 in (value1,value2) GROUP BY col1 ORDER BY col2 DESC"

  Was this answer useful?  Yes

Andrej

  • Aug 20th, 2015
 

Every field in SELECT statement needs to be in GROUP BY also - unless it is an aggregate function (SUM, MAX, MIN, etc...). Not necessary for ORDER BY

  Was this answer useful?  Yes

Papadon

  • Jul 26th, 2016
 

Yes, We can.

Code
  1. SELECT COUNT(EMPNO), DEPTNO FROM EMP GROUP BY DEPTNO ORDER BY DEPTNO;

  Was this answer useful?  Yes

Anand

  • Oct 25th, 2016
 

Yes, GROUP BY must come before ORDER BY if both are used

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions