• Articles
  • Tutorials
  • Interview Questions

GROUP BY And ORDER BY in SQL

Tutorial Playlist

ORDER By in SQL and GROUP By Clause in SQL

In this SQL tutorial, we will learn how to use ORDER By and GROUP By in SQL. Group By in SQL is used to arrange similar data into groups and Order By in SQL is used to sort the data in ascending or descending order.

ORDER By in SQL

SQL Order By is used to sort the data in ascending or descending order. It sorts the data in ascending order by default. To sort the data in descending order we use the DESC keyword.

Syntax of ORDER By in SQL:

SELECT column1, column2….
FROM table_name
ORDER BY column1 ASC/DESC, column2 ASC/DESC;
Example:
Sort all the students in ascending order in SQL by the “marks” column.
SELECT Name
FROM Student_details
ORDER BY Roll_no ASC;

GROUP By in SQL:

It is used to arrange similar data into groups. The GROUP BY clause follows the WHERE clause comes before the ORDER BY clause.

The GROUP BY statement is utilized to group rows with similar values into summary rows, such as finding out the total number of Apples in each store. It is commonly combined with aggregate functions like COUNT(), MAX(), MIN(), SUM(), and AVG() to group the result set based on one or more columns.

Syntax of GROUP By in SQL:

SELECT column1, column 2…
FROM table_name
WHERE [condition]
GROUP BY column1, column2
ORDER BY column1, column2;

Check out our free video on SQL Group By Clause to learn how to group and summarize data effortlessly

Video Thumbnail

Example:
If we want to know the total marks of each student, then GROUP BY is used as follows:

SELECT Name, Sum(marks)
FROM Student_details
GROUP BY Name;

Get 100% Hike!

Master Most in Demand Skills Now!

Course Schedule

Name Date Details
SQL Training 14 Dec 2024(Sat-Sun) Weekend Batch View Details
21 Dec 2024(Sat-Sun) Weekend Batch
28 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Data Engineer

As a skilled Data Engineer, Sahil excels in SQL, NoSQL databases, Business Intelligence, and database management. He has contributed immensely to projects at companies like Bajaj and Tata. With a strong expertise in data engineering, he has architected numerous solutions for data pipelines, analytics, and software integration, driving insights and innovation.