Difference Between Order By and Group By Clause in SQL
Last Updated :
24 Dec, 2024
SQL provides powerful tools for organising and analysing data, and two commonly used clauses are ORDER BY
and GROUP BY
. The ORDER BY
clause is used to sort data, while the GROUP BY
clause is used to group rows with similar values, often combined with aggregate functions like SUM()
, AVG()
, or COUNT()
.
Understanding the difference between these clauses is crucial for writing efficient and accurate SQL queries. In this article, we will explore the differences between these clauses, their syntax, and practical examples to help us use them effectively.
Difference Between ORDER BY and GROUP BY Clause
The following table summarises the key differences:
GROUP BY | ORDER BY |
---|
Group by statement is used to group the rows that have the same value. | Whereas Order by statement sort the result-set either in ascending or descending order. |
It may be allowed in CREATE VIEW statement. | While it does not use in CREATE VIEW statement. |
In select statements, it is always used before the order by keyword. | While in the select statement, it is always used after the group by keyword. |
An attribute cannot be in the group by a statement under the aggregate function. | Whereas in order by statement, the attribute can be under aggregate function. |
In group by clause, the tuples are grouped based on the similarity between the attribute values of tuples. | Whereas in order by clause, the result set is sorted based on ascending or descending order. |
Group by controls the presentation of tuples(rows). | While order by clause controls the presentation of columns. |
Order By
Order by keyword sort the result-set either in ascending or descending order. This clause sorts the result set in ascending order by default. In order to sort the result-set in descending order DESC keyword is used.
Syntax:
SELECT column_1, column_2, column_3...........
FROM Table_Name
ORDER BY column_1, column_2, column_3....... ASC|DESC;
Key Terms
- Table_Name: Name of the table.
- ASC: keyword for ascending order.
- DESC: keyword for descending order.
Employees table creation
Here, we create an employees table with employee_id,first_name, and salary and with the help of order by clause, we can sort the element by their specific column.
Query:
create table employees (
employee_id INT PRIMARY KEY ,
first_name VARCHAR(50) ,
salary INT
);
INSERT INTO employees(employee_id,first_name,salary)
VALUES
(100,'Steven',24000),
(101,'Neena',17000),
(102,'Lex',17000),
(103,'John',11000),
(104,'Robert',12000),
(105,'Leo',10000);
Output:
employee TableSorting in Ascending Order
It sorts the records automatically in ascending order if we want to show the records in descending order then we use DESC.
Query:
select * from employees ORDER BY salary;
Output:
Table 1Sorting in Descending Order
The ORDER BY
salary
DESC
query sorts the rows in the employees
table based on the salary
column in descending order, displaying the highest salaries first.
Query:
select * from employees ORDER BY salary DESC;
Output
table 2Group By
Group by statement is used to group the rows that have the same value. It is used with aggregate functions for example AVG(), COUNT(), SUM()etc. One thing is to remember about the group by clause that the tuples are grouped based on the similarity between the attribute values of tuples.
Syntax
SELECT function_Name(column_1), column_2
FROM Table_Name
WHERE condition
GROUP BY column_1, col umn_2
ORDER BY column_1, column_2;
Key Terms
- function_Name: Name of the aggregate function (
SUM()
, AVG()
, COUNT()
, etc.)
- Table_Name: Name of the table.
- GROUP BY: Groups rows with the same values in the specified columns.
Using COUNT()
The COUNT() function is used when we need to return the total number of rows that are stored in the database. So the example for the COUNT() function is
Query:
SELECT COUNT(Salary) from employee;
Output
table 3Using AVG()
The AVG() function calculates the average value of a specified column. This query computes the average salary of all employees in the employees
table.
Query:
SELECT AVG(Salary) from employees;
Output
table 4Using SUM()
The SUM() function calculates the total sum of a specified column. this query returns the total salary paid to all employees in the employees
table.
Query:
SELECT SUM(Salary) from employees;
Output
table 5Conclusion
Understanding how to use GROUP BY and ORDER BY statements is important for sorting the data and results. Whether we want to organize our data in ascending or descending order then we have to use the ORDER BY clause and if we want to organize multiple results under one group then use GROUP BY Clause. Both clauses can be used together to group and then sort the aggregated data. Mastery of these clauses will enhance our ability to query and analyse data effectively.
Similar Reads
Difference Between PARTITION BY and GROUP BY in PL/SQL
In Oracle PL/SQL, effective data manipulation is crucial for achieving optimal performance in database operations. Two essential SQL clauses often used to organize data are PARTITION BY and GROUP BY. Although both are used for structuring and analyzing data, they function quite differently and are u
5 min read
Difference Between Having Clause and Group by Clause
SQL is a powerful tool for data analysis, and mastering the nuances of the GROUP BY and HAVING clauses is essential for writing efficient queries. These clauses work together to group and filter data, enabling users to derive meaningful insights from datasets. In this article, we will explore the de
4 min read
Difference between From and Where Clause in SQL
1. FROM Clause: It is used to select the dataset which will be manipulated using Select, Update or Delete command.It is used in conjunction with SQL statements to manipulate dataset from source table.We can use subqueries in FROM clause to retrieve dataset from table. Syntax of FROM clause: SELECT *
2 min read
Difference Between Cube and Rollup in SQL Server
In SQL Server, both ROLLUP and CUBE are sub-clause of the GROUP BY clause and are used in conjunction with aggregate functions to produce summary reports. It helps to generate multiple group sets using the hierarchy. To enhance the capabilities of grouping and aggregation, SQL Server provides two po
3 min read
Difference Between Where and Group By
WHERE and GROUP BY clauses are essential tools for filtering and organizing data in SQL queries. While both are used to refine the output of a query, they serve distinct purposes. In this article, we will explore the differences between WHERE and GROUP BY clauses, including their syntax, use cases,
4 min read
Difference Between Distinct and Group By in PL/SQL
In PL/SQL, knowing the difference between DISTINCT and GROUP BY is important for working with data effectively. Although DISTINCT and GROUP BY might seem similar, they serve different purposes. In this article, we'll explore DISTINCT and GROUP BY Clause with the syntax and various examples along the
5 min read
Difference between Where and Having Clause in SQL
In SQL, the WHERE and HAVING clauses are essential for filtering data and refining query results. While both serve the purpose of applying conditions, they are used at different stages of query execution and for distinct purposes. Understanding the differences between the WHERE and HAVING clauses is
4 min read
Difference between Inner Join and Outer Join in SQL
JOINS in SQL are fundamental operations used to combine data from multiple tables based on related columns. They are essential for querying data that is distributed across different tables, allowing you to retrieve and present it as a single or similar result set.In this article, We will learn about
5 min read
Difference between View and Cursor in SQL
1. View : A view is a virtual table that not actually exist in the database but it can be produced upon request by a particular user. A view is an object that gives the user a logical view of data from a base table we can restrict to what user can view by allowing them to see an only necessary colum
3 min read
Difference between Simple and Complex View in SQL
A View in SQL as a logical subset of data from one or more tables. Views are used to restrict data access. A View contains no data of its own but it is like a window through which data from tables can be viewed or changed. The table on which a View is based is called BASE Tables. There are 2 types o
2 min read