DEV Community

Cover image for Mastering SQL: Understanding Group By and Order By
lateef sulaiman
lateef sulaiman

Posted on

Mastering SQL: Understanding Group By and Order By

Introduction
In this article, I'll walk you through two of the most important concepts in SQL: GROUP BY and ORDER BY. Mastering these concepts will make query writing much easier and more efficient for you. We will explore how to use them, when to use them, and where they can be applied effectively. By the end of this article, you'll have a clear understanding of these concepts and be able to use them proficiently in your SQL queries.

GROUP BY is an essential tool for Data Analysis. GROUP BY groups rows with the same values in specified columns into summary rows. It is mostly used with ‘Aggregate Functions’ like ‘COUNT’, ‘SUM’, ‘AVG’, ‘MAX’, and ‘MIN’.
Think of GROUP BY as sorting things into different categories. Imagine you have a big box filled with balls of different colors and sizes. To organize them, you decide to separate them into smaller boxes based on their color. You would end up with a box of blue balls, a box of red balls, and so on. In this scenario, you are grouping the balls by their color. Similarly, you could also organize the balls by their size, placing all the small balls in one box, the medium balls in another, and the large balls in yet another. This is exactly what GROUP BY does in SQL: it organizes your data into categories based on one or more columns.
Example
Suppose I have a table named 'Student_Data' with the following data:

Name Gender Birth_Month Age
Mike Male January 12
Alex Male January 14
Anita Female April 12
Gbenga Male February 15
Amanda Female June 14
Folarin Male February 13
Lateef Male September 10
Oyiza Female September 10
Mark Male May 11
Valerie Female June 13
Chioma Female April 15

I can group these data by Gender, Birth_Month, Age and even Name.

Example Queries and Results:

  1. Group by Gender ```

SELECT Gender
FROM Student_Data
GROUP BY Gender;

Result: 

Gender
------
|Male   |
|Female |

2. 
Enter fullscreen mode Exit fullscreen mode

SELECT Birth_Month
FROM Student_Data
GROUP BY Birth_Month;

Result:

Birth_Month
-----------
|January|
|April|
|February|
|June|
|September|
|May|

3.
Enter fullscreen mode Exit fullscreen mode

SELECT name
FROM Student_Data
GROUP BY name;

Result:

Name
--------
|Mike|
|Alex|
|Anita|
|Gbenga|
|Amanda|
|Folarin|
|Lateef|
|Oyiza|
|Mark|
|Valerie|
|Chioma|

**Group By and Aggregate Functions**
Now, let's talk about GROUP BY and Aggregate Functions. These are used to aggregate data, providing summarized information from your dataset.

Still going by the above scenario with the colored balls, if we are interested in knowing the details of each box, we can use what we call Aggregate Functions. How does the Aggregate Function work? With Aggregate Functions, we can determine the number of balls in each box, the biggest ball in each box, the smallest ball in each box, the average size of the balls in each box, and the total size of the balls in each box.

In SQL, Aggregate Functions like COUNT, MAX, MIN, AVG, and SUM are used with the GROUP BY clause to perform these calculations.

From the Student_Data above, we can use GROUP BY and Aggregate Functions to calculate the number of students by gender, the average age of students, the age of the oldest student, the age of the youngest student, and the sum of the students' ages by gender.

Example Queries and Results:

- Number of students by gender: We can use the COUNT function to know the number of students by gender.
Enter fullscreen mode Exit fullscreen mode

SELECT Gender, COUNT(*) AS Number_of_Students
FROM Student_Data
GROUP BY Gender;

Result:

| Gender | Number_of_Students 
|------- | ------  
| Male   |  6   
| Female |  5   
|    |          



- Average Age of Students by Gender: We can use the 'AVG' function to calculate the average age of students by gender.

Enter fullscreen mode Exit fullscreen mode

SELECT Gender, AVG(Age) AS Average_Age
FROM Student_Data
GROUP BY Gender;

Result: 

| Gender | Average_Age
| ------ | -------
| Male   | 12.5
| Female | 12.8
|        | 


- Oldest Student by Gender: We can use the 'MAX' function to find the age and name of the Oldest Student.

Enter fullscreen mode Exit fullscreen mode

SELECT Gender, MAX(Age) AS Oldest_Student
FROM Student_Data
GROUP BY Gender;

Result: 

| Gender | Oldest_Student
| ------ | ----------
| Male   | 15
| Female | 15
|        | 


- Sum of Students' ages by gender: We can use the 'SUM' function to calculate the total ages of Students by Gender.

Enter fullscreen mode Exit fullscreen mode

SELECT Gender, SUM(Age) AS Total_Ages
FROM Student_Data
GROUP BY Gender;


Result:


| Gender | Total_Ages
| ------ | -------
| Male   | 75
| Female | 64

By using 'GROUP BY' with AGGREGATE FUNCTIONs we can easily get summarized insights, helping us to better analyze and understand our data. 

**ORDER BY**
Now that we understand how to use 'GROUP BY' to organize data into categories, let's talk about 'ORDER BY'.'ORDER BY' in SQL is used to arrange data in ascending or descending order based on the values specified. 

Think of 'ORDER BY' as arranging a list of names alphabetically or arranging a group of students by their height.

Example: Considering our 'Student_Data' table. We can decide to list the students by their ages in ascending order.

Enter fullscreen mode Exit fullscreen mode

SELECT Name,Age
FROM Student_Data
ORDER BY Age;

Result:

| Name    | Age
|------   | -----
| Lateef  | 10
| Oyiza   | 10
| Mark    | 11
| Mike    | 12
| Anita   | 12
| Folarin | 13
| Valerie | 13
| Alex    | 14
| Amanda  | 14
| Gbenga  | 15
| Chioma  | 15 

**Combining GROUP BY and ORDER BY**
We can combine 'GROUP BY' and 'ORDER BY' to get more insight from our 'Student_Data' Table.

We can find the average age of students for each gender and then list these gender groups in descending order of their average age.

Example Query:

Enter fullscreen mode Exit fullscreen mode

SELECT Gender, AVG(Age) AS Average_Age
FROM Student_Data
GROUP BY Gender
ORDER BY Average_Age DESC;

Result:

| Gender | Average_Age
| ------ | ---------
| Female | 12.8
| Male   | 12.5

In the query above:

- The 'GROUP BY' clause groups the student by their Gender 

- The 'AVG' function calculates the average age for each gender group.

- The 'ORDER BY' clause sorts the result by the average age in descending order. 

By combining 'GROUP BY' and 'ORDER BY' we can sort our grouped data, making it easier to analyze and understand the results. 

**CONCLUSION**
Understanding how to use 'GROUP BY' and 'ORDER BY' in SQL is crucial for effective Data Analysis. These powerful tools allow us to organize, summarize and sort data in meaningful ways. 'GROUP BY' can help us  categorize our data into groups, Aggregate Functions can help us extract valuable insights from these groupings. The ORDER BY clause then helps us to sort and present our results in an ordered fashion, making our data easier to interpret and analyze.

Enter fullscreen mode Exit fullscreen mode

Top comments (0)