Briefly explain the use of group by in sql statements

Briefly explain the use of group by in sql statements

1. Overview

Group by means to group data according to the rules after by. The so-called grouping means dividing the data set into several "small groups" and processing them accordingly.

2. Grammatical rules

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

3. Examples

We have an order table like this:

We want to count the total amount of each user's order, we can use group by to achieve this:

select Customer,sum(OrderPrice) as total_price group by Customer;

The following result set appears:
Bush 2000
Carter 1700
Adams 2000
In fact, the data is first grouped by Customer, then the sum of orderPrice of each group is calculated, and finally each group is displayed as a row.
If you remove the group by statement, the following results will appear:

It is about adding up all the prices to get the final total, which is different from our expectations.

4. Note

If the following appears:

select category, sum(quantity) as sum of quantity, summaryfrom Agroup by categoryorder by categorydesc

An error will be reported because the selected field must either be included after group by as the basis for grouping, or be included in an aggregate function, otherwise the summary column cannot match a row.
Therefore, the following can be used:

select category, sum(quantity) AS sum of quantities from Agroup by category order by sum(quantity) desc

If you want to group by multiple columns, you can use the group by all syntax:

select Customer,OrderDate,sum(OrderPrice) group by all Customer,OrderDate

5. The difference between where and having

where is to remove rows that do not meet the where condition before query grouping, that is, filter data before grouping. The where condition cannot contain aggregate functions.
Having is to filter the groups that meet the conditions, that is, to filter the data after grouping. The having statement often contains aggregate functions, and having is used to filter out specific groups.
Example:

select Customer,sum(OrderPrice) as total_price group by Customer having total_price>1700;

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • This article will show you how to use group by in the database
  • MySQL group by method for single word grouping sequence and multi-field grouping
  • Tutorial on how to use group by in pymongo

<<:  Restart the Docker service to apply the automatic start and stop command (recommended)

>>:  Teach you how to use vscode to build a react-native development environment

Recommend

Detailed explanation of MySQL combined query

Using UNION Most SQL queries consist of a single ...

InnoDB engine redo file maintenance method

If you want to adjust the size and number of Inno...

Detailed explanation of the properties and functions of Vuex

Table of contents What is Vuex? Five properties o...

Vue implements paging function

This article example shares the specific code of ...

Using JS to implement a small game of aircraft war

This article example shares the specific code of ...

WeChat applet implements countdown for sending SMS verification code

This article shares the specific code for the WeC...

Some references about colors in HTML

In HTML, colors are represented in two ways. One i...

Docker Gitlab+Jenkins+Harbor builds a persistent platform operation

CI/CD Overview CI workflow design Git code versio...

How to configure nginx+php+mysql in docker

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

20 excellent foreign web page color matching cases sharing

This article collects 20 excellent web page color ...

Solve the problem of Docker starting Elasticsearch7.x and reporting an error

Using the Docker run command docker run -d -p 920...

Causes and solutions for MySQL deadlock

The database, like the operating system, is a sha...

Windows 2016 Server Security Settings

Table of contents System update configuration Cha...

Analyze the method of prometheus+grafana monitoring nginx

Table of contents 1. Download 2. Install nginx an...

CSS Sticky Footer Implementation Code

This article introduces the CSS Sticky Footer imp...