GROUP BY And ORDER BY in SQL - SQL Tutorial - Intellipaat (2024)

Order By 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.

GROUP BY And ORDER BY in SQL - SQL Tutorial - Intellipaat (1)

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.

Watch this video on Order by Clause in SQL

Syntax of Order By in SQL:

SELECT column1, column2….FROM table_nameORDER BY column1 ASC/DESC, column2 ASC/DESC;

Example:
Sort all the students in ascending order in SQL by the “marks” column.

SELECT NameFROM Student_detailsORDER BY Roll_no ASC;

Still, have queries? Come to Intellipaat’s SQL Community, clarify all your doubts, and excel in your career!

Group By in SQL:

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

Watch this video on Group By Clause in SQL

Syntax of Group By in SQL:

SELECT column1, column 2…FROM table_nameWHERE [condition]GROUP BY column1, column2ORDER BY column1, column2;

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

Want to get certified in SQL! Learn SQL from top SQL experts and excel in your career with intellipaat’s SQL certification.

SELECT Name, Sum(marks)FROM Student_detailsGROUP BY Name;

Get 100% Hike!

Master Most in Demand Skills Now !

GROUP BY And ORDER BY in SQL - SQL Tutorial - Intellipaat (2024)

FAQs

Can I have GROUP BY and ORDER BY together in SQL query? ›

Can I have GROUP BY and ORDER BY together in SQL query?

How to use GROUP BY and order clause in SQL? ›

How to use GROUP BY and order clause in SQL?

What is the best practice for GROUP BY in SQL? ›

What is the best practice for GROUP BY in SQL?

What is the difference between GROUP BY and ORDER BY in SQL w3schools? ›

What is the difference between GROUP BY and ORDER BY in SQL w3schools?

Top Articles
Latest Posts
Article information

Author: Corie Satterfield

Last Updated:

Views: 5970

Rating: 4.1 / 5 (42 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Corie Satterfield

Birthday: 1992-08-19

Address: 850 Benjamin Bridge, Dickinsonchester, CO 68572-0542

Phone: +26813599986666

Job: Sales Manager

Hobby: Table tennis, Soapmaking, Flower arranging, amateur radio, Rock climbing, scrapbook, Horseback riding

Introduction: My name is Corie Satterfield, I am a fancy, perfect, spotless, quaint, fantastic, funny, lucky person who loves writing and wants to share my knowledge and understanding with you.