How to use GROUP BY in SQL?

Learn to Code Today!

Learn 10x faster: coding, no-code and data skills. Join millions of users mastering new tech skills and accelerating their career with Enki.
Get started

Efficiently grouping and aggregating data is a key skill in SQL. It's crucial for data analysis, reporting, and business intelligence. The GROUP BY clause helps perform complex aggregations and gain insights from data sets. In this tutorial, we'll explore how to harness the power of GROUP BY.

At enki.com, we help you become proficient in SQL and other programming languages through interactive coding challenges and tutorials.

Understanding GROUP BY in SQL

The GROUP BY statement, used alongside aggregate functions like COUNT, SUM, AVG, MAX, and MIN, groups result sets by one or more columns.

Use Cases for GROUP BY:

  1. Data Summarization: Efficiently summarize large data sets.
  2. Reporting: Perfect for reports needing grouped statistics, such as sales per region or average scores per student.

Simple Example: Finding Unique Values Using GROUP BY

Let's start with a basic example.

This query retrieves unique department names from the employees table. It groups rows with the same department name, ensuring uniqueness.

Using Aggregate Functions with GROUP BY

Now let's delve deeper with aggregate functions:

Example using COUNT:

This query groups employees by department. It counts how many employees exist in each department, providing an employee summary.

Example using SUM:

Here, we calculate the total salary expenditure per department. The SUM function helps in financial aggregation.

GROUP BY with Multiple Columns

Let's refine our analysis using more than one column:

In this case, we group by department and job_title. This aggregates the dataset into finer segments like the number of developers in the IT department.

You can also use multiple aggregate functions at the same time:

This query provides various statistics: total employees, average salary, highest salary, and lowest salary per department. It's a comprehensive way to view the data.

Wrapping Up

Mastering the GROUP BY clause is vital for database administrators, data analysts, and software developers. It allows you to perform complex data manipulations and derive meaningful conclusions.

Continue your learning journey with us at enki.com. Our interactive lessons are designed to enhance your coding skills across various languages and platforms.

About Enki

  • Fully personalized online up-skilling
  • Unlimited AI coaching
  • Designed by Silicon Valley experts

More articles

Meet your AI-enabled coach

Professional athletes have a coach for every aspect of their performance. Why can’t you for your work? Enki’s AI-powered coaching on-demand - combined with state of the art, structured learning content - makes this a reality.
1
1:1 AI Coaching
How do I remove duplicate emails?
Convert the list to a set and back to a list. Sets automatically remove duplicates.
2
Personalized Exercises
3
Interactive practice

Unlock full access to all skills on Enki with a 7-day free trial

Get started