[MySQL] Insert query results, aggregate functions, and group queries for basic queries

In the last article, I summarized the basic addition, deletion, query and modification syntax. In this article, I will describe the second half of the basic query.

1. Insert query results

The grammar of a single query has been understood in the previous article, let's start to look at the
scenario where two query statements are used in combination Grammar:

INSERT INTO table_name [(column [, column ...])] SELECT ...

Case: Delete duplicate records in the table, that is, there can only be one copy of duplicate data

---- 创建带有重复数据的数据表
mysql> CREATE TABLE duplicate_table (id int, name varchar(20));
-- 插入测试数据
mysql> INSERT INTO duplicate_table VALUES
    -> (100, 'aaa'),
    -> (100, 'aaa'),
    -> (200, 'bbb'),
    -> (200, 'bbb'),
    -> (200, 'bbb'),
    -> (300, 'ccc');
--查询表中信息
mysql> select * from duplicate_table;
+------+------+
| id   | name |
+------+------+
|  100 | aaa  |
|  100 | aaa  |
|  200 | bbb  |
|  200 | bbb  |
|  200 | bbb  |
|  300 | ccc  |
+------+------+

--创建另一张表
mysql> create table no_duplicate_table like duplicate_table;
--原表去重后的数据插入到新表中
mysql> insert into no_duplicate_table select distinct *from duplicate_table;
--查询新表信息
mysql> select * from no_duplicate_table;
+------+------+
| id   | name |
+------+------+
|  100 | aaa  |
|  200 | bbb  |
|  300 | ccc  |
+------+------+
--修改表名,实现原子的去重
mysql> rename table duplicate_table to old_duplicate_table,no_duplicate_table TO duplicate_table;

mysql> select * from duplicate_table;
+------+------+
| id   | name |
+------+------+
|  100 | aaa  |
|  200 | bbb  |
|  300 | ccc  |
+------+------+

The purpose of modifying the table name by rename here is to wait for the operation of the table to be completed, put it in, update it, take effect, and save time

2. Aggregation function

Aggregate functions in MySQL are often used to calculate and count data. The following are several common aggregate functions

function illustrate
COUNT([DISTINCT] expr) Returns the number of queried data
SUM([DISTINCT] expr) Returns the sum of the queried data, not numbers are meaningless
AVG([DISTINCT] expr) Returns the average value of the queried data, not numbers are meaningless
MAX([DISTINCT] expr) Returns the maximum value of the queried data, not numbers are meaningless
MIN([DISTINCT] expr) Returns the minimum value of the queried data, not numbers are meaningless

Count the number of students in the class

mysql> select * from stu;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  1 | 唐三藏    |     134 |   98 |      56 |
|  3 | 猪悟能    |     176 |   98 |      90 |
|  4 | 曹孟德    |     140 |   90 |      67 |
|  5 | 刘玄德    |     110 |  115 |      45 |
|  6 | 孙权      |     140 |   73 |      78 |
|  7 | 宋公明    |     150 |   95 |      30 |
+----+-----------+---------+------+---------+

--统计行数
mysql> select count(1) from stu;
mysql> select count(*) from stu;
+----------+
| count(1) |
+----------+
|        6 |
+----------+

Count how many math grades there are in the class (remove duplicates)

mysql> select math from stu;
+------+
| math |
+------+
|   98 |
|   98 |
|   90 |
|  115 |
|   73 |
|   95 |
+------+

--正确的去重:先去重后聚合
mysql> select count(distinct math) from stu;
+----------------------+
| count(distinct math) |
+----------------------+
|                    5 |
+----------------------+

--错误的去重:先聚合后去重
mysql> select distinct  count( math) from stu;
+--------------+
| count( math) |
+--------------+
|            6 |
+--------------+
mysql> select sum(math) from stu;
+-----------+
| sum(math) |
+-----------+
|       569 |
+-----------+

1. Count the total score of math scores
2. Count the average score of math scores
3. Count the number of people who failed English scores
4. Return the highest score in English
5. Return the lowest score in mathematics > 70 points

--1
mysql> select sum(math) from stu;
+-----------+
| sum(math) |
+-----------+
|       569 |
+-----------+
1 row in set (0.00 sec)
-----2
mysql> select avg(math) from stu;
+-------------------+
| avg(math)         |
+-------------------+
| 94.83333333333333 |
+-------------------+

mysql> select sum(math)/count(*) from stu;
+--------------------+
| sum(math)/count(*) |
+--------------------+
|  94.83333333333333 |
+--------------------+
-----3
mysql> select count(*) from stu where english<60;
+----------+
| count(*) |
+----------+
|        3 |
+----------+
--4
mysql> select max(english) from stu;
+--------------+
| max(english) |
+--------------+
|           90 |
+--------------+
--5
mysql> select min(math) from stu where math>70;
+-----------+
| min(math) |
+-----------+
|        73 |
+-----------+

3. Group query ----group by&having

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;


Discuss the EMP employee table
DEPT department table
SALGRADE salary grade table in combination with the case
insert image description here
dept table


emp table


salgrade table


Display the average salary and maximum salary of each department
group by 'column name': grouping is based on the same column and different rows of data; after grouping, the [group column name such as deptno] in each group must be the same and can be aggregated and compressed

mysql> select deptno,max(sal) 最高,avg(sal)平均 from emp group by deptno;
+--------+---------+-------------+
| deptno | 最高    | 平均        |
+--------+---------+-------------+
|     10 | 5000.00 | 2916.666667 |
|     20 | 3000.00 | 2175.000000 |
|     30 | 2850.00 | 1566.666667 |
+--------+---------+-------------+

Display the average and minimum wages for each job type in each sector

mysql> select deptno,job,avg(sal)平均,min(sal) 最低  from emp group by deptno,job;
+--------+-----------+-------------+---------+
| deptno | job       | 平均        | 最低    |
+--------+-----------+-------------+---------+
|     10 | CLERK     | 1300.000000 | 1300.00 |
|     10 | MANAGER   | 2450.000000 | 2450.00 |
|     10 | PRESIDENT | 5000.000000 | 5000.00 |
|     20 | ANALYST   | 3000.000000 | 3000.00 |
|     20 | CLERK     |  950.000000 |  800.00 |
|     20 | MANAGER   | 2975.000000 | 2975.00 |
|     30 | CLERK     |  950.000000 |  950.00 |
|     30 | MANAGER   | 2850.000000 | 2850.00 |
|     30 | SALESMAN  | 1400.000000 | 1250.00 |
+--------+-----------+-------------+---------+

Note: The fields that appear after group by can appear after select, and there are aggregation functions. The rest may not necessarily [select ename, deptno, job, avg(sal) average, min(sal) lowest from emp group by deptno, job;] will report an error, because ename is not an aggregation condition

3.1 The difference between having and where

having is generally used with group by to
display the department whose average salary is lower than 2000 and its average salary

--这里先进行聚合压缩统计,having再开始条件筛选
mysql> select deptno,avg(sal) deptavg from emp group by deptno having deptavg<2000;
+--------+-------------+
| deptno | deptavg     |
+--------+-------------+
|     30 | 1566.666667 |
+--------+-------------+

Except for SMITH, show the average salary and its average salary for each job in each department whose average salary is lower than 2000
insert image description here

Fourth, the execution order of SQL queries

The order of execution of each keyword in the SQL query: from > on> join > where > group by > with > having > select>distinct > order by > limit

Five, OJ practice

  1. Batch insert datainsert image description here
insert into actor values(1,"PENELOPE","GUINESS","2006-02-15 12:34:33"),(2,"NICK","WAHLBERG","2006-02-15 12:34:33");
  1. Find out the current salary salary situation of all employeesinsert image description here
select distinct salary from salaries order by salary desc;
  1. Find all information about the latest employeeinsert image description here
select * from employees order by hire_date desc limit 1;
  1. Find all information about the employee with the third-last-last entry timeinsert image description here
select * from employees
 where hire_date=(
    select distinct hire_date from employees order by  hire_date desc limit 2,1
    );
  1. Find the employee number emp_no with more than 15 salary records and the corresponding record times t
    grouping + aggregation function
select emp_no,count(*) t from salaries group by emp_no having t>15;
  1. Get grouped by title from the titles table
    insert image description here
select title,count(title) t from titles group by title having t>=2;
  1. Find duplicatesinsert image description here
select email from Person group by email having count(email)>1;
  1. find big countryinsert image description here
select name,population,area from World where area>=3000000 or population>=25000000;
  1. Given an Employee table, find the Nth highest salary (Salary)
    insert image description here
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
  SET N = N - 1;
  RETURN (
     
      select distinct(Salary) as getNthHighestSalary
      from Employee
      GROUP BY Salary 
      ORDER BY Salary DESC 
      limit 1 offset N
  );
END

Guess you like

Origin blog.csdn.net/m0_54469145/article/details/131579557