GROUP BY vs ORDER BY
The GROUP BY clause and ORDER BY clause in SQL are used to arrange data obtained by SQL queries. The important difference between the GROUP BY clause and ORDER BY clause is that the GROUP BY clause is applied when we want to use an aggregate function in SQL query on more than one set of rows. In contrast, the ORDER BY clause is applied when we want to receive the data obtained by the query in the sorted order. Before moving towards comparison charts, we will first understand these SQL clauses.
GROUP BY Clause
The GROUP BY statement in SQL uses queries to arrange data within attribute values. Usually, we use the GROUP BY clause with the SELECT statement. We have to place the GROUP BY clause after the WHERE clause always. Additionally, it is also paced before the ORDER BY clause.
We can often use the GROUP BY clause with aggregate functions like SUM, AVG, MIN, MAX, and COUNT to produce output from the database. The query for the GROUP BY clause is grouped query, and it returns a single row for each grouped object.
Below is the Syntax to use the GROUP BY Clause in a SQL query
SELECT COLUMNNAME, FUNCTION(COLUMNNAME) FROM TABLENAME WHERE CONDITION GROUP BY COLUMNNAME;
Let’s understand the GROUP BY clause with the help of a few examples. Consider the following tables along with the given records.
Table: Employees
EMPLOYEEID | FIRST_NAME | LAST_NAME | SALARY | CITY | DEPARTMENT | MANAGERID | WORKING_HOURS | GENDER |
1001 | VAIBHAVI | MISHRA | 65000 | PUNE | ORACLE | 1 | 12 | F |
1002 | VAIBHAV | SHARMA | 60000 | NOIDA | ORACLE | 1 | 9 | M |
1003 | NIKHIL | VANI | 50000 | JAIPUR | FMW | 2 | 10 | M |
2001 | PRACHI | SHARMA | 55500 | CHANDIGARH | ORACLE | 1 | 10 | F |
2002 | BHAVESH | JAIN | 65500 | PUNE | FMW | 2 | 12 | M |
2003 | RUCHIKA | JAIN | 50000 | MUMBAI | TESTING | 4 | 9 | F |
3001 | PRANOTI | SHENDE | 55500 | PUNE | JAVA | 3 | 9 | F |
3002 | ANUJA | WHERE | 50500 | JAIPUR | FMW | 2 | 9 | F |
3003 | DEEPAM | JAUHARI | 58500 | MUMBAI | JAVA | 3 | 12 | M |
4001 | RAJESH | GOUD | 60500 | MUMBAI | TESTING | 4 | 10 | M |
Example 1: Suppose we want to know employees sum salary in a particular city for the above data. To do this, we will execute the below query as follow:
SELECT CITY, SUM(SALARY) FROM EMPLOYEES GROUP BY CITY;
This query initially formed a result that has grouped the city. Next, the SUM function is performed on each group of cities, and finally, we will get the desired results as shown below:

Example 2: Suppose we want to know employees' max salary in a particular department for the above data. To do this, we will execute the below query as follow:
SELECT DEPARTMENT, MAX(SALARY) FROM EMPLOYEES GROUP BY DEPARTMENT;
This query initially formed a result that has grouped the department. Next, the MAX function is performed on each group of the department, and finally, we will get the desired results as shown below:

ORDER BY Clause
The GROUP BY clause and the ORDER BY clause could be used with the SELECT statement. In SQL queries, we used the ORDER BY clause to sort the records returned by the query in ascending or descending order. If we didn't mention the sorting order, the entire result would be sorted by default in ascending order.
Below is the Syntax to use the ORDER BY Clause in a SQL query
SELECT * FROM TABLENAME ORDER BY [ASC | DESC];
Let us understand the ORDER BY clause with the help of a few examples. Consider the following tables along with the given records.
Table: Employees
EMPLOYEEID | FIRST_NAME | LAST_NAME | SALARY | CITY | DEPARTMENT | MANAGERID | WORKING_HOURS | GENDER |
1001 | VAIBHAVI | MISHRA | 65000 | PUNE | ORACLE | 1 | 12 | F |
1002 | VAIBHAV | SHARMA | 60000 | NOIDA | ORACLE | 1 | 9 | M |
1003 | NIKHIL | VANI | 50000 | JAIPUR | FMW | 2 | 10 | M |
2001 | PRACHI | SHARMA | 55500 | CHANDIGARH | ORACLE | 1 | 10 | F |
2002 | BHAVESH | JAIN | 65500 | PUNE | FMW | 2 | 12 | M |
2003 | RUCHIKA | JAIN | 50000 | MUMBAI | TESTING | 4 | 9 | F |
3001 | PRANOTI | SHENDE | 55500 | PUNE | JAVA | 3 | 9 | F |
3002 | ANUJA | WHERE | 50500 | JAIPUR | FMW | 2 | 9 | F |
3003 | DEEPAM | JAUHARI | 58500 | MUMBAI | JAVA | 3 | 12 | M |
4001 | RAJESH | GOUD | 60500 | MUMBAI | TESTING | 4 | 10 | M |
Example 1: Suppose we want to arrange the result in the sorted order, either ascending or descending, based on the first name column. In that case, we would like to use the ORDER BY clause to get the result. To do this, we will execute the below query as follow:
SELECT * FROM EMPLOYEES ORDER BY FIRST_NAME ASC;

Key Difference between GROUP BY and ORDER BY
The following are the points that differentiate between the GROUP BY and ORDER BY clauses:
- The GROUP BY statement in SQL is used to group the records based on a similar value in a particular column. On the other hand, the ORDER BY clause in SQL is used to sort the display results in ascending or descending order.
- While performing a query using the GROUP BY clause in SQL query, it’s compulsory to use the aggregate function. On the other hand, while using the ORDER BY clause in SQ, it's not compulsory to use an aggregate function.
- The GROUP BY clause is always placed in SQL query after the WHERE clause but before the ORDER BY clause. On the other hand, the ORDER BY clause is always placed after the GROUP BY clause.
Comparison Chart
SR.NO | GROUP BY | ORDER BY |
1 | The GROUP BY clause is used to group the rows with similar values. | The ORDER BY clause is used to sort the result either in descending or ascending order |
2 | It controls the presentation of rows | It controls the presentation of columns |
3 | THE GROUP BY clause is always placed before the ORDER BY clause. | ORDER BY clause is always placed after the GROUP BY clause. |
4 | It is compulsory to use an aggregate function | Not compulsory to use an aggregate function |
5 | The attribute cannot be under the aggregate function | in the GROUP BY statement. The attribute can be under the aggregate function in the ORDER BY statement. |