ORDERBY alters the order of the specified column into the table by giving there fields name and arrange them in desc as well as asc ordering but default ordering is asc while in GROUPBY aggregates the records by the specified column which allow to perform the aggregation on non-grouped columns.
Examples of ORDERBY and GROUPBY clause:
Consider a TABLE name emp having fields Id and Name:
Table name emp:
ID NAME
1 sam
2 John
3 ken
4 sam
First Query:
SELECT * FROM emp ORDER BY NAME
So the result of the above query is:
3 ken
2 John
1 sam
4 sam
Now the second Query using the GroupBy clause:
SELECT Count(ID), NAME
FROM TABLE
GROUP BY NAME
The result of the above query is:
1 ken
1 John
2 sam
- ORDERBY clause shows a field in asc as well as desc while in GROUPBY shows the same fieldnames, ids in only one output.
- GROUP BY clause often used with the aggregate functions like SUM, AVG, MAX, MIN and COUNT on the columns in a query while ORDERBY is used to sort the result set.
- ORDERBY basically controls the presentation of the column for the results While GROUPBY clause controls the presentation of the rows.
- In OREDRBY clause order of columns make difference.
For example:
ORDERBY A,B,C and ORDERBY C,A,B both returns the same number of records but the row order will be different.
while in GROUPBY order of column make no difference.
For example:
GROUPBY A,B,C and GROUPBY C,A,B both returns the same result.
- ORDERBY clause is not allowed in the CREATE VIEW statement while the GROUPBY cluase can be used in the CREATE VIEW statement to sort the data.
- In SELECT statement the OREDRBY clause is always places after the GROUPBY clause while in case of GROUPBY is always placed before the ORDERBY clause in the SELECT statement.
0 Comment(s)