.NET/C#
  • ASP.NET Core Overview
  • ASP.NET Core API tutorial
    • How to create ASP.NET Core C# API tutorial
      • launchSettings
      • Install swagger
        • Swagger best practices
      • Run the app
      • Fix CORS problem
      • Add AutoMapper
      • Add JWT reference
      • Add .gitignore
      • Basic structure & EF Core
      • AddSingleton
    • Loading Related Entities
  • Unit test controller logic in ASP.NET Core
    • Unit testing controllers
  • .NET Q&A
    • dotnet try
    • LINQ
      • LINQ Query Syntax
      • Lambda Expression
      • Standard Query Operators
  • .NET C# Interview questions
    • C# - .NET interview questions and answers [Part 1]
    • C# - .NET interview questions and answers [Part 2]
    • C# Interview questions [Part 3] General questions
  • C#
    • Object-Oriented Programming (Principles)
      • N-tier architecture style
      • Command and Query Responsibility Segregation (CQRS) pattern
      • Project architecture
    • C# Advanced review
      • Implicit typing
      • Generics
      • Attributes
      • Reflection
      • Delegates
      • Anonymous Methods and Lambda Expressions
      • Events
      • Ref vs Out
      • Task
        • TaskFactory Class
  • MySQL
    • MySQL Lerning
      • SELECT
      • ORDER BY
      • WHERE
      • DISTINCT
      • IN
      • BETWEEN
      • Join
      • INNER JOIN
      • LEFT JOIN
      • RIGHT JOIN
      • GROUP BY
      • Subquery
      • UNION
    • Stored Procedures
      • CREATE PROCEDURE
  • Versioning API, MongoDB and ci-cd
    • Create a web API with ASP.NET Core and MongoDB
    • REST API versioning with ASP.NET Core
    • Design a CI/CD pipeline using Azure DevOps
      • Create a CI/CD pipeline for GitHub repo using Azure DevOps Starter
  • TFS & TFCV
    • What is Team Foundation Version Control
      • Develop and share your code in TFVC using Visual Studio
      • Suspend your work and manage your shelvesets
    • Newtonsoft Json.NET
      • Serializing and Deserializing JSON
  • MS-SQL
    • Quick tutorial
      • Add new column to a table
      • LEFT/RIGHT Reverse operator
      • Dates (Transact-SQL)
      • CAST and CONVERT (Transact-SQL)
      • Types of JOIN
      • Our first Left Outer Join
      • CROSS JOIN
Powered by GitBook
On this page
  • MySQL GROUP BY examples
  • The GROUP BY clause: MySQL vs. standard SQL

Was this helpful?

  1. MySQL
  2. MySQL Lerning

GROUP BY

PreviousRIGHT JOINNextSubquery

Last updated 4 years ago

Was this helpful?

The GROUP BY clause groups a set of rows into a set of summary rows by values of columns or expressions. The GROUP BY clause returns one row for each group. In other words, it reduces the number of rows in the result set.

You often use the GROUP BY clause with such as , , , , and . The aggregate function that appears in the SELECT clause provides information about each group.

The GROUP BY clause is an optional clause of the statement. The following illustrates the GROUP BY clause syntax:

SELECT 
    c1, c2,..., cn, aggregate_function(ci)
FROM
    table
WHERE
    where_conditions
GROUP BY c1 , c2,...,cn;

The GROUP BY clause must appear after the FROM and WHERE clauses. Following the GROUP BY keywords is a list of comma-separated columns or expressions that you want to use as criteria to group rows.

MySQL evaluates the clause after the FROM, and SELECT clauses and before the , and clauses:

MySQL GROUP BY Evaluation Order

MySQL GROUP BY examples

Let’s take some example of using the GROUP BY clause.

A) Simple MySQL GROUP BY example

Suppose you want to group values of the order’s status into subgroups, you use the GROUP BY clause with the status column as the following query:

SELECT 
    status
FROM
    orders
GROUP BY status;
SELECT DISTINCT
    status
FROM
    orders;

B) Using MySQL GROUP BY with aggregate functions

For example, if you want to know the number of orders in each status, you can use the COUNT function with the GROUP BY clause as follows:

SELECT 
    status, COUNT(*)
FROM
    orders
GROUP BY status;

See the following orders and orderdetails table.

SELECT 
    status, 
    SUM(quantityOrdered * priceEach) AS amount
FROM
    orders
INNER JOIN orderdetails 
    USING (orderNumber)
GROUP BY 
    status;

Similarly, the following query returns the order numbers and the total amount of each order.

SELECT 
    orderNumber,
    SUM(quantityOrdered * priceEach) AS total
FROM
    orderdetails
GROUP BY 
    orderNumber;

C) MySQL GROUP BY with expression example

In addition to columns, you can group rows by expressions. The following query gets the total sales for each year.

SELECT 
    YEAR(orderDate) AS year,
    SUM(quantityOrdered * priceEach) AS total
FROM
    orders
INNER JOIN orderdetails 
    USING (orderNumber)
WHERE
    status = 'Shipped'
GROUP BY 
    YEAR(orderDate);

D) Using MySQL GROUP BY with HAVING clause example

SELECT 
    YEAR(orderDate) AS year,
    SUM(quantityOrdered * priceEach) AS total
FROM
    orders
INNER JOIN orderdetails 
    USING (orderNumber)
WHERE
    status = 'Shipped'
GROUP BY 
    year
HAVING 
    year > 2003;

The GROUP BY clause: MySQL vs. standard SQL

Standard SQL does not allow you to use an alias in the GROUP BY clause, however, MySQL supports this.

For example, the following query extracts the year from the order date. It first uses year as an alias of the expression YEAR(orderDate) and then uses the year alias in the GROUP BY clause. This query is not valid in standard SQL.

SELECT 
    YEAR(orderDate) AS year, 
    COUNT(orderNumber)
FROM
    orders
GROUP BY 
    year;

MySQL also allows you to sort the groups in ascending or descending orders while the standard SQL does not. The default order is ascending. For example, if you want to get the number of orders by status and sort the status in descending order, you can use the GROUP BY clause with DESC as the following query:

SELECT 
    status, 
    COUNT(*)
FROM
    orders
GROUP BY 
    status DESC;

Notice that we used DESC in the GROUP BY clause to sort the status in descending order. We could also specify explicitly ASC in the GROUP BY clause to sort the groups by status in ascending order.

In this tutorial, we have shown you how to use the MySQL GROUP BY clause to group rows into subgroups based on values of columns or expressions.

Let’s take a look at the orders table in the .

table orders
MySQL GROUP BY example

As you can see, the GROUP BY clause returns unique occurrences of status values. It works like the operator as shown in the following query:

The allow you to perform the calculation of a set of rows and return a single value. The GROUP BY clause is often used with an aggregate function to perform calculation and return a single value for each subgroup.

MySQL GROUP BY with COUNT function
order-orderDetails-tables

To get the total amount of all orders by status, you the orders table with the orderdetails table and use the SUM function to calculate the total amount. See the following query:

MySQL GROUP BY with SUM function
MySQL GROUP BY order number example
MySQL GROUP BY expression example

In this example, we used the function to extract year data from order date ( orderDate). We included only orders with shipped status in the total sales. Note that the expression which appears in the SELECT clause must be the same as the one in the GROUP BY clause.

To filter the groups returned by GROUP BY clause, you use a clause. The following query uses the HAVING clause to select the total sales of the years after 2003.

MySQL GROUP BY with HAVING example
MySQL GROUP BY SQL standard
MySQL GROUP BY DESC example
sample database
DISTINCT
aggregate functions
join
YEAR
HAVING
aggregate functions
SUM
AVG
MAX
MIN
COUNT
SELECT
GROUP BY
WHERE
HAVING
ORDER BY
LIMIT