Database_06_Group query

#Advanced5: Group query

Syntax:
select group function, column (required to appear after group by)
from table
[where filter condition]
group by grouped list
[order by clause]

Note: The
query list must be special, and the requirements are the grouping function and the field after group by

Features:
1. Grouping query filtering is divided into two categories.
Before grouping, filter the original table group by clause in front of where
grouping and filter the grouped result set after group by clause having
2. The condition of grouping function must be placed in the having clause in
front of the packet 3 can be screened, priority
4 supports a single field of the packet, a plurality of packet fields, expression or function
5 may also add sorting (on the last)

#Case 1: Query the maximum salary of each type of work

SELECT MAX(salary) 最高工资,job_id 工种 
FROM employees 
GROUP BY job_id 
ORDER BY 最高工资;

#Case 2: Query the number of departments at each location

SELECT COUNT(*),location_id 
FROM departments 
GROUP BY location_id;

#Added group front filter condition
#Case 1: query the average salary of each department with a character in the mailbox

SELECT AVG(salary),department_id
FROM employees 
WHERE email LIKE '%a%'
GROUP BY department_id;

#Case 2: Query the maximum salary of each leader's employee with bonus

SELECT MAX(salary),manager_id
FROM employees
WHERE commission_pct IS NOT NULL
GROUP BY manager_id;

#Added grouped filter condition
#Case 1: Query the number of employees in which department>2

SELECT COUNT(*),department_id
FROM employees
GROUP BY department_id
HAVING COUNT(*)>2;

#Case 2: Query the maximum salary of employees with bonuses for each type of work> 12000 type number and salary
#1. Query the maximum salary of each type of work with bonuses

SELECT MAX(salary),job_id
FROM employees
WHERE commission_pct IS NOT NULL
GROUP BY job_id;

#2. Continue to filter by 1, the highest salary is> 12000

SELECT MAX(salary),job_id
FROM employees
WHERE commission_pct IS NOT NULL
GROUP BY job_id
HAVING MAX(salary)>12000;

#Case 3: Query the minimum wage of each leader with a leader number>102>5000 which is the leader number and its minimum wage

SELECT MIN(salary),manager_id
FROM employees
WHERE manager_id>102
GROUP BY manager_id
HAVING MIN(salary)>5000;

# According to expression or function grouping
# Case: group by the length of the employee name, query the number of employees in each group, and filter the number of employees> 5

SELECT COUNT(*),LENGTH(last_name)
FROM employees
GROUP BY LENGTH(last_name)
HAVING COUNT(*)>5;

#By multiple field grouping
#Case: Query the average salary of employees of each type of work in each department

SELECT AVG(salary),department_id,job_id
FROM employees
GROUP BY department_id,job_id;/*两者一样分为一组*/

#Add Sort
#Case: Query the average salary of employees of each type of work in each department, and display according to the salary level

SELECT AVG(salary),department_id,job_id
FROM employees
GROUP BY department_id,job_id
ORDER BY AVG(salary) DESC;

#test

SELECT MAX(salary),MIN(salary),AVG(salary),SUM(salary),job_id
FROM employees
GROUP BY job_id
ORDER BY job_id;

SELECT MIN(salary),manager_id
FROM employees
WHERE manager_id IS NOT NULL
GROUP BY manager_id
HAVING MIN(salary)>=6000;

SELECT department_id,AVG(salary),COUNT(*)
FROM employees
GROUP BY department_id
ORDER BY AVG(salary);

SELECT COUNT(*),job_id
FROM employees
GROUP BY job_id;

Guess you like

Origin blog.csdn.net/Yungang_Young/article/details/104521437