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 group and Order By in SQL is is used to sort the data in the ascending or descending order.
SQL Order By is used to sort the data in the ascending or descending order. It sorts the data in ascending order by default. To sort the data in descending order we use DESC keyword.
Syntax of Order By in SQL:
SELECT column1, column2…. FROM table_name ORDER BY column1 ASC/DESC, column2 ASC/DESC;
Sort all the students in the ascending order in SQL by the “marks” column.
SELECT Name FROM Student_details ORDER BY Roll_no ASC;
It is used to arrange similar data into group. The GROUP BY clause follows the WHERE clause and comes before the ORDER BY clause.
Syntax of Group By in SQL:
SELECT column1, column 2… FROM table_name WHERE [condition] GROUP BY column1, column2 ORDER BY column1, column2;
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;Previous Next
Download Interview Questions asked by top MNCs in 2019?