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

Table paging function implemented by Vue2.0+ElementUI+PageHelper

Preface I have been working on some front-end pro...

Vue basic instructions example graphic explanation

Table of contents 1. v-on directive 1. Basic usag...

Summary of bootstrap learning experience-css style design sharing

Due to the needs of the project, I plan to study ...

Centos7 installation of Nginx integrated Lua sample code

Preface The computer I use is a Mac, and the oper...

How to change the password of mysql5.7.20 under linux CentOS 7.4

After MySQL was upgraded to version 5.7, its secu...

How to install MySQL and MariaDB in Docker

Relationship between MySQL and MariaDB MariaDB da...

How to implement rounded corners with CSS3 using JS

I found an example when I was looking for a way t...

Example of removing json backslash in php

1. Remove backslashes through the "stripslas...

MySQL optimization tutorial: large paging query

Table of contents background LIMIT Optimization O...

A brief discussion on JavaScript throttling and anti-shake

Table of contents Throttling and anti-shake conce...

How to configure nginx+php+mysql in docker

First, understand a method: Entering a Docker con...

How to make your own native JavaScript router

Table of contents Preface Introduction JavaScript...

3 ways to correctly modify the maximum number of connections in MySQL

We all know that after the MySQL database is inst...

How does Zabbix monitor and obtain network device data through ssh?

Scenario simulation: The operation and maintenanc...