SQL GROUP BY detailed explanation and simple example

SQL GROUP BY detailed explanation and simple example

The GROUP BY statement is used in conjunction with the Aggregate function to group the result set based on one or more columns.

SQL GROUP BY Syntax

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;

Demo Database

In this tutorial, we will use the well-known Northwind sample database.

Here is the data selected from the "Orders" table:

OrderID CustomerID EmployeeID OrderDate ShipperID
10248 90 5 1996-07-04 3
10249 81 6 1996-07-05 1
10250 34 4 1996-07-08 2

Data from the "Shippers" table:

ShipperID ShipperName Phone
1 Speedy Express (503) 555-9831
2 United Package (503) 555-3199
3 Federal Shipping (503) 555-9931

Data selected from the "Employees" table:

EmployeeID LastName FirstName BirthDate Photo Notes
1 Davolio Nancy 1968-12-08 EmpID1.pic Education includes a BA....
2 Fuller Andrew 1952-02-19 EmpID2.pic Andrew received his BTS....
3 Leverling Janet 1963-08-30 EmpID3.pic Janet has a BS degree....

SQL GROUP BY Example

Now we want to find the number of orders delivered by each delivery person.

The following SQL statement classifies and counts orders by delivery person:

SELECT Shippers.ShipperName,COUNT(Orders.OrderID) AS NumberOfOrders FROM Orders
LEFT JOIN Shippers
ON Orders.ShipperID=Shippers.ShipperID
GROUP BY ShipperName;

GROUP BY more than one column

We can also apply GROUP BY clause to more than one column as shown below:

SELECT Shippers.ShipperName, Employees.LastName,
COUNT(Orders.OrderID) AS NumberOfOrders
FROM ((Orders
INNER JOIN Shippers
ON Orders.ShipperID = Shippers.ShipperID)
INNER JOIN Employees
ON Orders.EmployeeID = Employees.EmployeeID)
GROUP BY ShipperName,LastName;

Thank you for reading, I hope it can help you, thank you for your support of this site!

You may also be interested in:
  • Mysql uses group by group sorting
  • Solution to MySQL 5.7 group by new feature error 1055
  • The difference between order by and group by in sql
  • mysql group by having example code
  • Detailed explanation of MYSQL GROUP BY usage
  • Incorrect use of SQL statement Groupby in Mysql
  • How to use GROUP BY in MySQL to get the first N records
  • MySQL optimizes GROUP BY (loose index scan vs compact index scan)
  • Detailed explanation of the implementation principle of MySQL group query Group By

<<:  A practical record of handling the ddgs and qW3xT.2 mining viruses implanted in Linux servers

>>:  Vue implements a simple calculator

Recommend

SQL to implement time series dislocation restoration case

Table of contents 1. Requirements description 2. ...

CSS animation property usage and example code (transition/transform/animation)

During development, a good user interface will al...

Summary of ways to implement single sign-on in Vue

The project has been suspended recently, and the ...

How to encapsulate WangEditor rich text component in Angular

The rich text component is a very commonly used c...

Example of implementing a virtual list in WeChat Mini Program

Table of contents Preface analyze Initial Renderi...

Commonly used English fonts for web page creation

Arial Arial is a sans-serif TrueType font distribu...

Summary of the differences between MySQL storage engines MyISAM and InnoDB

1. Changes in MySQL's default storage engine ...

JavaScript implements class lottery applet

This article shares the specific code of JavaScri...

mysql8 Common Table Expression CTE usage example analysis

This article uses an example to describe how to u...

A brief comparison of Props in React

Table of contents Props comparison of class compo...

WeChat applet wxs date and time processing implementation example

Table of contents 1. Timestamp to date 2. Convert...

MySQL 5.7 installation and configuration tutorial under CentOS7 64 bit

Installation environment: CentOS7 64-bit MINI ver...

CSS solves the misalignment problem of inline-block

No more nonsense, post code HTML part <div cla...