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:
Data from the "Shippers" table:
Data selected from the "Employees" table:
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:
|
<<: A practical record of handling the ddgs and qW3xT.2 mining viruses implanted in Linux servers
>>: Vue implements a simple calculator
Table of contents 1. Requirements description 2. ...
During development, a good user interface will al...
The project has been suspended recently, and the ...
The rich text component is a very commonly used c...
Table of contents Preface analyze Initial Renderi...
Arial Arial is a sans-serif TrueType font distribu...
1. Changes in MySQL's default storage engine ...
Docker officially recommends that we use port map...
This article shares the specific code of JavaScri...
This article uses an example to describe how to u...
Table of contents Props comparison of class compo...
Table of contents 1. Timestamp to date 2. Convert...
Installation environment: CentOS7 64-bit MINI ver...
After installing the latest version 8.0.11 of mys...
No more nonsense, post code HTML part <div cla...