Learn MySQL with teacher Wang: aggregate functions

Source: Internet
Author: User

Learn MySQL with teacher Wang: aggregate functions
Teacher: Wang Shaohua QQ Group No.: 483773664 Learning Content

Usage of COUNT (), SUM (), AVG (), MAX (), and Min () four aggregate functions


The aggregate functions include count (), SUM (), AVG (), MAX (), and Min ().

One, COUNT ()
(a) Introduction

The count () function is used to count the number of records.

(ii) examples

Use the count () function to count the number of records for an employee table

1 SELECTCOUNT(*)  FROMemployee ;

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M01/83/E3/wKiom1d_MrnCYqpxAAAKsk8GQgo370.png " >

Second, SUM () function (i.) INTRODUCTION

The sum () function is the SUM function. Use the sum () function to calculate the sum of a field value in a table.

(ii) Example 1 data preparation
1234567891011121314151617181920 CREATETABLEgrade(     num  INT(10)  NOT NULL ,    course  VARCHAR(10)  NOT NULL ,    score  FLOAT    );INSERT INTO grade VALUES(1001,‘数学‘,80);INSERT INTO grade VALUES(1001,‘语文‘,90);INSERT INTO grade VALUES(1001,‘英语‘,85);INSERT INTO grade VALUES(1001,‘计算机‘,95);INSERT INTO grade VALUES(1002,‘数学‘,88);INSERT INTO grade VALUES(1002,‘语文‘,90);INSERT INTO grade VALUES(1002,‘英语‘,89);INSERT INTO grade VALUES(1002,‘计算机‘,90);INSERT INTO grade VALUES(1003,‘数学‘,80);INSERT INTO grade VALUES(1003,‘语文‘,98);INSERT INTO grade VALUES(1003,‘英语‘,85);INSERT INTO grade VALUES(1003,‘计算机‘,95);
2 Student's total

Use the sum () function to count the total number of students in the grade table, which is 1001.

1 selectnum,sum(score) from grade wherenum=1001;

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M02/83/E3/wKiom1d_MrmwmG2tAAAKJlIRH3Y619.png " >

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/83/E2/wKioL1d_MrnQnVUKAAAPzVmY_0g931.png " >

The result of a manual total is the same as using the SUM function.

3. Attention

The sum () function can only calculate fields of numeric types. such as int, float, double, decimal, and so on.

Third, AVG () function
(a) Introduction

The AVG () function is a function of averaging

(ii) examples

Use the AVG () function to calculate the average age in the employee table

1 selectavg(age) fromemployee;

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M02/83/E2/wKioL1d_MrqipQK0AAAIEuJhnMc507.png " >

Four, Max () functions
(a) Introduction

Use the max () function to find the maximum value of a field in a table.

(ii) Example 1, using the max () function to query the maximum age in the employee table
1 select  max(age) from employee;

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/83/E3/wKiom1d_MrqRW8LRAAAH9zYJGhg312.png " >

2. The max () function applies not only to numeric types, but also to string types

The principle of comparison: The ASCII code is calculated, that is, the word line a minimum, the letter Z maximum

Comparison method: Compare the first letter first, if the first letter is the same, and then continue to compare the next letter.


Data preparation

12345678910 CREATE TABLE work(    id INT PRIMARY KEY UNIQUE,    name VARCHAR(10),    sex VARCHAR(4),    info TEXT    ); INSERT INTO work VALUES(1001,‘hjh‘,NULL,NULL);INSERT INTO work VALUES(1002,‘cch‘,NULL,NULL);INSERT INTO work VALUES(1003,‘zk‘,NULL,‘student‘);

Use the max () function to query the maximum value of the Name field in the work table

1 selectmax(name) fromwork;

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02

Learn MySQL with teacher Wang: aggregate functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.