Thursday, October 11, 2012

Difference between ORDER BY and GROUP BY

ORDER BY

ORDER BY is used to sort the rows.It affects only the order of result set.
SELECT FIRST_NAME,LAST_NAME,JOB_ID,DEPARTMENT_ID FROM EMPLOYEE
ORDER BY JOB_ID
NOTE:If we are using ORDER BY clause,it must be last clause of the SQL statement.

GROUP BY
GROUP BY is used to divide the rows into group in a table. GROUP BY specifies how the rows should be grouped.
SELECT DEP_ID,AVG(SAL) FROM EMPLOYEE
GROUP BY DEP_ID
NOTE:Rows are sorted by ascending order of the columns included in the GROUP BY clause by default.

No comments:

Copyright © Codingnodes,2014.All Rights Reserved.