Skip to main content
Log inGet a demo
Back to SQL Dictionary
Basic statements and clauses

SQL GROUP BY

What is the SQL GROUP BY Clause?

The GROUP BY clause in SQL is a powerful function used to group rows from a table based on the values of one or more columns. It is often used in conjunction with aggregate functions (such as COUNT, SUM, AVG, MAX, or MIN) to perform calculations on these grouped data. GROUP BY is a fundamental tool for summarizing and analyzing data in relational databases.

When to Use GROUP BY?

You would use the GROUP BY clause when you need to:

  1. Aggregate data: When you want to calculate summary statistics, like the total sales, average scores, or the count of items that meet certain criteria.

  2. Group similar data: When you want to categorize rows into meaningful groups based on one or more columns, such as grouping sales by product categories, employees by department, or customers by location.

  3. Simplify large datasets: When you have a large dataset and you want to reduce the number of rows by summarizing and condensing information.

Syntax

The basic syntax of the GROUP BY clause is as follows:

SELECT column1, aggregate_function(column2)
FROM table_name
GROUP BY column1;
  • SELECT: The columns you want to retrieve.
  • column1: The column by which you want to group the data.
  • aggregate_function: Functions like SUM, COUNT, AVG, MAX, or MIN that operate on the grouped data.
  • table_name: The name of the table from which you want to retrieve data.

You can also use multiple columns in the GROUP BY clause to create more granular groups.

Parameter Values

  • column1: The column by which you want to group the data. It can be of any data type (numeric, text, date, etc.).
  • aggregate_function: Various aggregate functions can be used, depending on what you want to calculate (e.g., SUM, COUNT, AVG, MAX, MIN).

Example Query

Let's consider a simple example. Suppose we have a table named sales with columns product, category, and revenue. We want to find the total revenue for each product category.

SELECT category, SUM(revenue) as total_revenue
FROM sales
GROUP BY category;

Example Table Response

The result of the above query might look like this:

categorytotal_revenue
Electronics15000
Clothing8000
Books4500

Use Cases

GROUP BY is used in a wide range of scenarios, including but not limited to:

  1. Sales reports: Summarizing sales data by product, region, or time period.
  2. Employee reports: Grouping employees by department or job title to calculate average salaries.
  3. Student reports: Grouping students by class to calculate class averages.
  4. Inventory management: Grouping inventory items by category to determine stock levels.
  5. Website analytics: Grouping website visits by date, user, or page to analyze traffic patterns.

SQL Dialects

The GROUP BY clause is a standard SQL feature supported by most relational database systems, including but not limited to MySQL, PostgreSQL, SQL Server, Oracle, and SQLite. While the basic syntax is consistent across these systems, some advanced features or specific functions may vary from one database to another.

Always consult the documentation for your specific database system for any unique implementations or extensions of the GROUP BY clause.

Related

SQL CASE

SQL HAVING

SQL IF

Ready to put your SQL knowledge to work?

Practice writing SQL to call data from the warehouse and sync it into Google Sheets in this 5 minute interactive demo.

Hightouch Audiences user interface.

Activate your data in less than 5 minutes