×

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 INSERT INTO Values

This article will help you in getting a better understanding of a very important SQL INSERT INTO VALUES function. INSERT INTO statement is used to insert or add a new record...

4 minutes read.

SQL SET Keyword

This article will provide you a good understanding of the Set keyword in Structured Query Language. What is the SET keyword? The SET keyword is used to specify values for the variables....

3 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 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.

How to use GROUP BY clause in SQL

In this SQL article, we will learn about the GROUP BY clause and how to use it in SQL. We will also discuss using the GROUP BY clause with the...

6 minutes read.

SQL SELECT Database

In this tutorial, we will help you to understand and learn how to select the database in SQL with the help of examples. The database user or the administrator wants to...

3 minutes read.

SQL DROP Database

This tutorial will help us understand how to drop a database from the SQL Server with a few examples. The DROP command removes all the objects stored in the database and...

4 minutes read.

SQL SELECT Statement

The SQL SELECT statement is used to retrieve the data from the tables. We can also retrieve the selected records from the table using the query condition. The SQL SELECT...

5 minutes read.

SQL Queries

In a database, queries are used to request the result set of data from the table or action on the records. A Query can answer your simple or complicated question, perform...

5 minutes read.

SQL Between Operator

SQL Between operator is a logical operator in the Structured Query Language. The Between operator is used to retrieve data within the range specified in the condition in the query. The...

7 minutes read.

Types of SQL JOIN

The SQL JOIN combines one or more than one tables based on their relationship. The SQL JOIN involves a parent table and a child table relationship. There are different types of...

10 minutes read.

SQL Top Clause

Top Clause: It returns the first ‘n’ number of rows as output. This Top clause is used to retrieve the required number of rows when there are thousands of records stored...

3 minutes read.

Nth highest salary

The most common and important question asked in interviews that how we can find the Nth highest salary in a table (2nd highest salary, 3rd highest salary, or Nth highest...

6 minutes read.

SQL Aggregate Operators

In SQL, the aggregate operators perform a calculation on multiple rows or multiple values of a single column and give the output a single value. Here, multiple rows of data are...

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 WHERE Clause

In this SQL section, we will help you understand the concept of the SQL SELECT WHERE clause with a few examples. The WHERE clause in SELECT query displays those records as...

5 minutes read.

SQL Cloning Tables

Cloning a Table: To create a copy of the table. To perform the operations, without affecting the actual table. Steps for creating a Cloning Table: Step 1: Empty Table Creation The syntax for Creating...

3 minutes read.

SQL CROSS Join

In this tutorial, we will help you understand the concept of the SQL CROSS Join clause with the few examples. The SQL CROSS Join query is used to join one or...

5 minutes read.

SQL Injection

SQL injection is a technique, this may destroy the database. It is one type of hacking technique. SQL IN WEB PAGES: Injection occurs when we ask for input like an id or...

3 minutes read.

SQL SELECT AVG

In this tutorial, we will learn about the aggregate function name avg() function concept in SQL with the help of examples. The AVG() function is one of the aggregate functions in...

3 minutes read.