Article Directory
This article mainly introduces the use of mysql's aggregation function and group by, and finally the practice of OJ topic.
aggregate function
Aggregate functions in MySQL are used to calculate and count data. Common aggregate functions include the aggregate functions listed below:
函数 说明
COUNT([DISTINCT] expr) 返回查询到的数据的数量
SUM([DISTINCT] expr) 返回查询到的数据的总和,不是数字没有意义
AVG([DISTINCT] expr) 返回查询到的数据的平均值,不是数字没有意义
MAX([DISTINCT] expr) 返回查询到的数据的最大值,不是数字没有意义
MIN([DISTINCT] expr) 返回查询到的数据的最小值,不是数字没有意义
For the aggregation functions listed above, let's use some cases to use the aggregation functions to enhance understanding, not much to say
- Count the number of students in the class
-- 使用 * 做统计,不受 NULL 影响
select count(*) from exam_result;
-- 使用表达式做统计
select count(1) from exam_result;
- How many math grades are there in the statistical class
select count(math) from exam_result;
But we have seen that the math scores are repeated, how to get rid of them? distinct
select distinct count(distinct math) from exam_result;
- Statistical Mathematics Total Score
select sum(math) from exam_result;
- Statistical Mathematics grade point average
select sum(math)/count(*) from exam_result;
select avg(math) from exam_result;
- Statistics of the number of people who fail English
select count(*) from exam_result where english<60;
- Return the highest score in English
select max(english) from exam_result;
- Returns > 70+ minimum score in mathematics
select min(math) from exam_result where math>70;
Group by group by use
The purpose of grouping is to facilitate aggregate statistics after grouping
Use the group by clause in select to perform group queries on specified columns
select column1, column2, .. from table group by column;
First create an employee information table
EMP employee table
DEPT department table
SALGRADE salary grade table
How to display the average salary and maximum salary of each department
- Show average and maximum salaries for each department
select deptno,max(sal) 最高,avg(sal) 平均 from emp group by deptno;
Grouping is to split a group into multiple groups according to conditions, and carry out statistical grouping within each group; it is to logically split a table into multiple sub-tables according to conditions, and then perform aggregation statistics on the respective sub-tables.
- Display the average and minimum wages for each job type in each sector
select deptno,job, avg(sal) 平均,min(sal) 最低 from emp group by deptno,job;
- Display the department with average salary below 2000 and its average salary
Statistics of the average salary of each department
select avg(sal) from EMP group by deptno
Having and group by are used together to filter the results of group by
select avg(sal) as myavg from EMP group by deptno having myavg<2000;
Having is to filter the aggregated statistical data by condition
- The difference between having and where is understood , and the order of execution
The stages of conditional filtering are different. Don't simply think that only when the table structure on the disk is imported into mysql, the real table is called a table. The intermediate screening and final results are all logical tables. It can be understood that everything in mysql is a table. In the future, as long as we can handle single-standard CURD well, we can use a unified method for all SQL scenarios.
After learning the above knowledge, let's do some OJ topic exercises below. The topics come from Niuke.com and leetcode. Do it to improve your ability to write sql
OJ topic
SQL228 batch insert data
describe
The topic has already executed the following statement:
drop table if exists actor; CREATE TABLE actor ( actor_id smallint(5) NOT NULL PRIMARY KEY, first_name varchar(45) NOT NULL, last_name varchar(45) NOT NULL, last_update DATETIME NOT NULL)
Please insert the following data in batches for the table actor (no 2 insert statements!)
actor_id first_name last_name last_update 1 PENELOPE GUINESS 2006-02-15 12:34:33 2 NICK WAHLBERG 2006-02-15 12:34:33
The sql statement is as follows:
insert into actor (actor_id,first_name,last_name,last_update) values (1,'PENELOPE','GUINESS','2006-02-15 12:34:33'),(2,'NICK','WAHLBERG','2006-02-15 12:34:33');
SQL202 Find out the current salary of all employees
describe
There is a salary table, and the salaries profile is as follows:
emp_no salary from_date to_date 10001 72527 2002-06-22 9999-01-01 10002 72527 2001-08-02 9999-01-01 10003 43311 2001-12-01 9999-01-01 Please find out the specific salary situation of all employees. For the same salary, it will be displayed only once, and displayed in reverse order. The output of the above example is as follows:
salary 72527 43311
The sql statement is as follows:
select distinct salary from salaries order by salary desc;
SQL195 Find all information of the latest employee
describe
There is an employee employees table profile as follows:
emp_no birth_date first_name last_name gender hire_date 10001 1953-09-02 Georgi Facello M 1986-06-26 10002 1964-06-02 Bezalel Simple F 1985-11-21 10003 1959-12-03 Part Bamford M 1986-08-28 10004 1954-05-01 Christian Koblick M 1986-12-01 Please find all the information of the latest employees in employees, the output of the above example is as follows:
emp_no birth_date first_name last_name gender hire_date 10004 1954-05-01 Christian Koblick M 1986-12-01
select * from employees order by hire_date desc limit 1;
SQL196 Find all the information of the employee whose entry time is the third last
describe
There is an employee employees table profile as follows:
emp_no birth_date first_name last_name gender hire_date 10001 1953-09-02 Georgi Facello M 1986-06-26 10002 1964-06-02 Bezalel Simple F 1985-11-21 10003 1959-12-03 Part Bamford M 1986-08-28 10004 1954-05-01 Christian Koblick M 1986-12-01 Please find all the information of the employee whose time of entry ranks the third last in employees. The output of the above example is as follows:
emp_no birth_date first_name last_name gender hire_date 10001 1953-09-02 Georgi Facello M 1986-06-26 Note: There may be employees who joined on the same date, so there may be more than one employee whose time is the third last.
select * from employees
where hire_date=(select distinct hire_date from employees order by hire_date desc limit 2,1);
SQL201 Find the employee number emp_no with more than 15 salary records and the corresponding number of records t
select emp_no,count(*) as t from salaries group by emp_no having t>15;
Get grouped by title from the titles table
select title,count(title) as t from titles
group by title having t>=2;
182. Find Duplicate Emails
select email from Person group by email having count(email)>1;
595. Big Country
select name,population,area from world
where area >= 3000000 or population >= 25000000;