SQL Tutorial for Beginners: Master GROUP BY with Practical Examples

SQL Tutorial for Beginners: Master GROUP BY with Practical Examples

Hey Guys! This is Dhyuthidhar, Welcome to my Blog. If you don’t know me...", you could say "Hi, I’m Dhyuthidhar, and if you're new here, welcome! I love writing about all things in Computer Science, especially in the realm of Machine Learning. Here's an interesting blog about the GROUP BY clause in SQL. I will explain the topic in detail and also try to explain it simply so that you can understand. We’ll explore GROUP BY examples to simplify your SQL learning journey.

Buckle up, Guys!

What is GROUP BY Clause?

  • The group by clause will group all the values in a column that have similar values in a column.

  • It is a clause used in SQL to get the aggregation for the data in SQL.

  • Let's say this is the example table. We can see these are about the products in the fruit store. We can see there are apples, oranges, and bananas. I want to know how much every product costs, like the cost of the apples I bought, with the total quantity and price.

  • Let's use GROUP BY here:

SELECT Product, SUM(Quantity) AS TotalQuantity, SUM(Quantity * Price) AS TotalRevenue FROM Sales GROUP BY Product;
  • Output -:

  • Here are the results we got, as we did group by(product), the products are grouped without duplication and as we sum quantity, we can see how much we brought each product and what is the total cost we spent on the product.

How do you group the data in SQL

  • It is the command used to aggregate the data in SQL.

  • Syntax -:

SELECT column1, aggregate_function(column2) FROM table_name GROUP BY column1;
  • SELECT -: Used to extract the data

  • column1 -: The column you want to group the data by

  • aggregate function -: these are the functions used to aggregate data like SUM(), COUNT(), MIN(), MAX().

  • table name -: This is the name of the table from where you want to extract and group the data.

Why we are using the GROUP BY clause

  • GROUP BY clause is a powerful clause used to aggregate the data.

  • Aggregation means summarizing the data into one value like MAX() or MIN() etc.

  • This will help to get useful insights.

  • As it will help to summarize the data we can get the hidden patterns and trends in the data

Key Benefits of GROUP BY Clause

  • Simplified Analysis -: Instead of analyzing the data row by row, we can analyze similar data by using GROUP BY and aggregating the data using functions like MAX(), MIN(), AVG() etc for deeper insights.

  • Summarization -: The data is grouped and summarized into categories, providing a bird’s-eye view of trends and patterns that might not be obvious otherwise.


Analogy: Organizing Your Bookshelf

Imagine your bookshelf is a chaotic mix of books—Math books, Science books, Fiction, Non-Fiction—all jumbled together. To make your collection easier to manage, you first group the books by category. Once grouped:

  • You can count the total number of books in each category (COUNT()).

  • Find the oldest book in a category (MIN()).

  • Calculate the average number of pages for books in each category (AVG()).

By organizing your books, you’ve effectively "GROUPED BY category" and summarized the collection to make decisions, such as which category needs more space or which books to lend.


Data Science Example: Detecting Outliers

Consider a dataset with numerical features (e.g., sales data) and a categorical column for product categories (e.g., electronics, clothing). To detect outliers:

  • Use GROUP BY to group the data by category.

  • Calculate aggregate metrics like MAX(), MIN(), AVG(), and STDEV() for each group.

  • Look for anomalies where values deviate significantly from the mean (e.g., 2 or 3 times the standard deviation).

This approach provides a first pass for identifying potential outliers in the dataset, though further analysis is required for confirmation.

How does GROUP BY work?

The GROUP BY operation will be done in 3 phases -: Split, Apply and Combine. Let's take the same analogy of organizing the bookshelf.

Split -:

  • Here we will split the data into categories.

  • Like I said we will split the books into Maths, Science books and Non-Fiction and Fictional books.

  • Similarly, in SQL GROUP BY divides the data into chunks(or groups) based on the selected column values.

Apply -:

  • Once the books are categorized, you can make calculations on them including:

    • Number of books in each category.

    • Average pages in each category.

    • Find the oldest book in each category.

  • In SQL these are equivalent to the aggregate functions like COUNT(), AVG() and MIN() to each group.

Combine -:

  • After performing all the calculations you will summarize them into a single list:

    • "Math: 10 books, oldest book: 2005."

    • "Fiction: 15 books, average pages: 300."

  • Similarly, in SQL, the results for each group are combined into a single output table, where each row represents a summarized view of a group.

Visualizing the example

Book NameCategoryPagesYear Published
Algebra BasicsMath2002015
Calculus AdvancedMath3002010
Astrophysics 101Science2502018
QuantumMechanicsScience3502012
Fictional StoriesFiction1502020
Novel of the YearFiction4002016

SQL Example:

Query:

SELECT category, COUNT(*) AS total_books, AVG(pages) AS avg_pages FROM bookshelf GROUP BY category;

Output:

CategoryTotal BooksAvg Pages
Math10250
Fiction15300
Non-Fiction8200

By applying the GROUP BY process, we’ve transformed a chaotic bookshelf into an organized summary that provides clear insights!

When to use GROUP BY

  • GROUP BY is useful when you want to summarize the data under the categorical level instead of checking row by row.

  • It is most commonly paired with aggregate functions like SUM, COUNT, AVG, MAX, or MIN to calculate meaningful summaries for each group.

  • Imagine you’re organizing your bookshelf. You don’t want to analyze every single book individually but instead want summaries for categories like Math, Science, and Fiction. For instance:

  • Total number of books in each category (COUNT).

  • The average number of pages for books in each category (AVG).

  • The oldest book in each category (MIN).

GROUP BY helps you gather these insights at the category level, making it easier to spot trends or patterns in your collection.


Where is GROUP BY used?

The GROUP BY clause is used within the SELECT statement. It follows a specific order -: - It comes after the WHERE clause (if filtering individual rows is needed). - It appears before the HAVING clause, which is used for filtering aggregated results.

  • Let's say in the bookshelf organization,

    • You want to remove some books from your analysis.(like damaged books—this corresponds to the WHERE clause).

    • Now you want to group the categories and apply the aggregation. (this is the GROUP BY step).

    • Finally, you decide if you want to focus only on categories meeting certain conditions, such as categories with at least 2 books (this is the HAVING clause).

GROUP BY operates between filtering individual rows and filtering aggregated results, ensuring you get summarized insights before applying further criteria.

Understanding the Difference Between WHERE and GROUP BY

  • The WHERE clause filters the data before grouping it; it is a pre-filter that determines which rows are included in the dataset for further processing. The WHERE clause can be used with SELECT, UPDATE, and DELETE statements. However, it cannot use aggregate functions in its conditions.

  • You are managing and organizing your books then you want to remove the old books from your cupboard, So you will check the books which are published before 1990.

SELECT book_name FROM books WHERE published_date < 1990;
  • And remove those books.

  • The GROUP BY clause is used to group rows that have the same values in specified columns.

  • It enables aggregate functions like SUM, COUNT, AVG, MAX, and MIN to calculate metrics for each group.

  • It organizes data so that one row represents each group, with aggregate calculations applied to that group.

  • If a column contains NULL values, GROUP BY treats all those NULL values as one group.

  • Once you’ve filtered old books, the GROUP BY clause helps you organize the remaining books by categories, such as Fiction, Science, or Math. For each category, you can calculate aggregate metrics like:

  • Total books per category (COUNT(book_name)).

  • The average price of books in each category (AVG(Price)).

  • The oldest book is published in each category (MIN(YearPublished)).

WHERE + GROUP BY Together

When WHERE and GROUP BY are used together:

  1. WHERE filters individual rows first (e.g., remove out-of-stock books).

  2. GROUP BY organizes the filtered rows into groups and performs aggregate calculations on those groups.

For example:
To find the average price of in-stock books in each category:

SELECT Category, AVG(Price)  FROM Books  WHERE quantity > 2  GROUP BY Category;

Bookstore Analogy:

  • WHERE clause is the step where you remove books that have a quantity of more than 2.

  • GROUP BY is the step where you organize the filtered books by category and calculate the average price per category.

This two-step process ensures that only relevant data is grouped and analyzed.

Conclusion

The GROUP BY clause is a powerful SQL tool for summarizing and organizing data into meaningful groups. Grouping similar rows and applying aggregate functions, it simplifies analysis and reveals trends. Whether filtering data with WHERE or refining results with HAVING, GROUP BY helps transform raw data into actionable insights, making it essential for effective data analysis.

Dive deeper into SQL! Explore more GROUP BY examples in our complete SQL tutorial for beginners. Try these queries out in your dataset. If you have any doubts or reviews, comment below!🙂