What is the Difference between GROUP and ORDER BY

What is the Difference between GROUP and ORDER BY

The GROUP and ORDER BY clauses are both used to organize data. But if we are talking about the difference between GROUP and ORDER BY clauses.
So we can say :
The ORDER BY clause’s use to sort the query result by specific columns.
The GROUP BY clause’s use to summarize unique combinations of columns values.

GROUP BY CLAUSE

GROUP BY is used in conjunction with the aggregate functions to group the result-set by one or more columns. Aggregate function in the SELECT list, like COUNT(), SUM(), MIN(), MAX(), AVG() etc.

Lets try understanding using example.

TABLE:
ID NAME
1  hari
2  shiv
3  laxmi
4  hari

Mysql_query(“SELECT Count(ID), NAME FROM TABLE GROUP BY NAME”);

Result :
1 shiv
1 laxmi 
2 hari

In This example we are applying GROUP BY Clause on name, in name field hari name repeat two time.

ORDER BY CLAUSE

ORDER BY is used for changing the order in which records are returned according to specified column. In other hand we can say ORDER BY Clause use to sort the data in ascending or descending order.

Lets try understanding using example.

TABLE:
ID NAME
1  hari
2  shiv
3  laxmi
4  hari

Mysql_query(“SELECT * FROM TABLE ORDER BY NAME”);

Result :
ID NAME
1  hari
4  hari
3  laxmi
2  shiv

In This example we are applying ORDER BY Clause on name, ORDER BY Clause applying sorting on name field and return result in a acceding order.
About Author

My name is Mukesh Jakhar and I am a Web Application Developer and Software Developer, currently living in Jaipur, India. I have a Master of Computer Application in Computer Science from JNU Jaipur University. I loves to write on technology and programming topics. Apart from this, I love to travel and enjoy the beauty of nature.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.