Group by and Order by in sql

We can learn how to use group by and order by in sql in this SQL tutorial. In SQL, Group By is used to organize related data into classes, and in SQL, Order By is used to arrange data in an ascending or descending order.

which sql keyword is used to sort the result-set?

Order By in SQL

SQL Order By is used for the ascending or descending order of data sorting. It filters the data by default in ascending order. We use the DESC keyword to identify the data in descending order.

Syntax of Order By in SQL:

SELECT column1,column2,....
FROM table_name
ORDER BY column1 ASC/DESC, column2 ASC/DESC;

EXAMPLE:

Filter all learners by the column “marks” in the ascending order in SQL.

SELECT Name 
FROM Student_Details
ORDER BY Roll_Number ASC;

Group By in SQL

It is used to classify related knowledge into classes. The WHERE clause is followed by the GROUP BY clause and comes before the ORDER BY clause.

Syntax of Group By in SQL:

SELECT column1, column2
FROM table_name
WHERE [condition]
GROUP BY column1, column2
ORDER By column1, column2;

Example:

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

SELECT name, marks
FROM student_details
GROUP BY name

Leave a Reply