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
Preface I have been working on some front-end pro...
Hexadecimal code table of various colors [Part 1] ...
Table of contents 1. v-on directive 1. Basic usag...
Due to the needs of the project, I plan to study ...
Preface The computer I use is a Mac, and the oper...
After MySQL was upgraded to version 5.7, its secu...
Relationship between MySQL and MariaDB MariaDB da...
I found an example when I was looking for a way t...
1. Remove backslashes through the "stripslas...
Table of contents background LIMIT Optimization O...
Table of contents Throttling and anti-shake conce...
First, understand a method: Entering a Docker con...
Table of contents Preface Introduction JavaScript...
We all know that after the MySQL database is inst...
Scenario simulation: The operation and maintenanc...