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
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!