×

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

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERIDWORKING_HOURSGENDER
1001VAIBHAVIMISHRA65000PUNEORACLE112F
1002VAIBHAVSHARMA60000NOIDAORACLE19M
1003NIKHILVANI50000JAIPURFMW210M
2001PRACHISHARMA55500CHANDIGARHORACLE110F
2002BHAVESHJAIN65500PUNEFMW212M
2003RUCHIKAJAIN50000MUMBAITESTING49F
3001PRANOTISHENDE55500PUNEJAVA39F
3002ANUJAWHERE50500JAIPURFMW29F
3003DEEPAMJAUHARI58500MUMBAIJAVA312M
4001RAJESHGOUD60500MUMBAITESTING410M

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:

GROUP BY vs ORDER BY

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:

GROUP BY vs ORDER BY

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

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERIDWORKING_HOURSGENDER
1001VAIBHAVIMISHRA65000PUNEORACLE112F
1002VAIBHAVSHARMA60000NOIDAORACLE19M
1003NIKHILVANI50000JAIPURFMW210M
2001PRACHISHARMA55500CHANDIGARHORACLE110F
2002BHAVESHJAIN65500PUNEFMW212M
2003RUCHIKAJAIN50000MUMBAITESTING49F
3001PRANOTISHENDE55500PUNEJAVA39F
3002ANUJAWHERE50500JAIPURFMW29F
3003DEEPAMJAUHARI58500MUMBAIJAVA312M
4001RAJESHGOUD60500MUMBAITESTING410M

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;
GROUP BY vs ORDER BY

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.NOGROUP BYORDER BY
1The 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
2It controls the presentation of rowsIt controls the presentation of columns
3THE GROUP BY clause is always placed before the ORDER BY clause.ORDER BY clause is always placed after the GROUP BY clause.
4It is compulsory to use an aggregate functionNot compulsory to use an aggregate function
5The 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.

Related Topics

SQL SubQuery

The Sub-query in the SQL is the inner query placed or positioned inside another query, which is also known is the outer query. The inner query is embedded in the...

6 minutes read.

SQL DROP Table

In this tutorial, we will help you to understand how to delete the table from the database in SQL with the help of examples. The DROP TABLE query is used to...

4 minutes read.

SQL INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query. There are...

3 minutes read.

SQL SELECT MAX

The SQL Max() function is an aggregate function in SQL. This function returns the values which are greater in the condition. The condition may be a number, or it may...

5 minutes read.

How to Create Temporary Table in SQL?

How to Create Temporary Table in SQL  Introduction to Temporary Tables Temporary table is a table which is used to store temporary data that can be used further in the same client...

3 minutes read.

DML Commands in SQL

DML is an abbreviation of Data Manipulation Language. Data Manipulation Language commands in Structured Query Language manipulate the data in the database. DML commands are used to retrieve records, add records,...

4 minutes read.

SQL Commands

SQL commands are classified into four groups on the basis of their nature. DDL (Data Definition Language) DML (Data Manipulation Language) DCL (Data Control Language) DQL (Data Query Language) NOTE: This...

1 minute read.

SQL SELECT AND Operator

This SQL tutorial explains and helps us understand how to use the AND Operator in the SELECT query with examples. The AND Operator is used to fetch the table’s records if...

2 minutes read.

How to delete a row in SQL

Introduction To delete or remove the unused/unwanted rows from a table, DELETE command is used in SQL.DELETE command removes the entire record from a table.The DELETE statement can delete one or...

6 minutes read.

How to compare date in SQL

In this section, we will learn about how dates can be compared in SQL. We can compare any random date with another date stored in a column of a table.This comparison...

4 minutes read.

How to use COUNT in SQL?

How to use COUNT in SQL Introduction COUNT( ) is an aggregate function in SQL.This function counts the number of records in a table if the condition is not specified.If the condition...

4 minutes read.

What is SQL Injection?

Introduction to SQL Injection SQL injection is a vulnerability or a technique that might destroy the database of a website or a web application. It is one of the most widely...

4 minutes read.

SQL Tutorial for Beginners

SQL tutorial provides basic and advanced concepts of Structured Query Language and how you deploy SQL to work with a relational database system. Our SQL tutorial is designed for beginners...

3 minutes read.

SQL Handling Duplicate

Removing Duplicates using DISTINCT Keyword: By using the DISTINCT keyword in SQL we can remove duplicate characters from tables or databases. A table contains more duplicate values and duplicate values can cause...

4 minutes read.

SQL Right Join

The SQL Right Join query displays all the table records and similar records from the left table. The query display zero records if it doesn’t find any similar records. If...

4 minutes read.

SQL Aggregate Functions

In this tutorial, we will learn and understand the SQL Aggregate Functions concept with the help of examples. SQL Aggregate Function is a function used to perform calculation operations on one...

4 minutes read.

SQL ORDER BY

SQL ORDER BY The SQL ORDER BY clause is used to sort the data stored in tables in the database. The sorting can be done in an ascending way, descending way,...

5 minutes read.

SQL NOT Operator

SQL NOT is a Boolean operator used with the WHERE clause. NOT operator shows the records if the expression is false. When we use the NOT operator, we fetch only...

7 minutes read.

SQL INSERT INTO SELECT

In this tutorial, we will help you to understand and learn how to copy records from one table and add them to another table in the SQL with the help...

5 minutes read.

Introduction to SQL

SQL Introduction SQL is Standard Query Language. This language is used to communicate or interact with database. In other words, SQL is used to access and manage data or information...

2 minutes read.