查询每个部门的编号,部门名称,部门人数,最高工资,最低工资,工资总和,平均工资
时间: 2024-10-24 13:07:25 浏览: 43
要查询每个部门的编号、部门名称、部门人数、最高工资、最低工资、工资总和以及平均工资,你需要结合多个查询或者使用子查询。假设有一个`employees`表记录员工信息,包括`dept_id`(部门编号)、`salary`(工资),还有一个`departments`表包含部门信息。这是一个复杂的查询示例:
```sql
SELECT d.dept_id AS department_id, d.department_name,
COUNT(e.emp_id) AS count_of_employees,
MAX(e.salary) AS max_salary, MIN(e.salary) AS min_salary,
SUM(e.salary) AS total_salary, AVG(e.salary) AS average_salary
FROM departments d
LEFT JOIN employees e ON d.dept_id = e.dept_id
GROUP BY d.dept_id, d.department_name;
```
这个查询首先通过`JOIN`将两个表连接起来,然后按部门分组计算所需信息。
相关问题
查询有上级员工的每个职业的人数,工资的总和,平均工资,最低工资
根据引用\[2\]中的查询语句,我们可以得到查询有上级员工的每个职业的人数,工资的总和,平均工资,最低工资的结果。查询语句如下:
SELECT job, COUNT(*) AS 人数, SUM(sal) AS 工资总和, AVG(sal) AS 平均工资, MIN(sal) AS 最低工资
FROM emp
WHERE mgr IS NOT NULL
GROUP BY job
ORDER BY 人数 DESC, 平均工资 ASC;
这条查询语句会按照人数降序排序,如果人数一致,则按照平均工资升序排序。这样可以得到每个职业的人数、工资总和、平均工资和最低工资的信息。
#### 引用[.reference_title]
- *1* *2* [SQL-Day04](https://blog.csdn.net/JJ_Zhangjian/article/details/93480681)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [一起学习数据库](https://blog.csdn.net/liuzhou110/article/details/114115032)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
如何查询员工ID为1001的信息? 如何查询所有员工的姓名和工资? 如何查询所有员工的入职日期和所在部门? 如何查询所有员工的年龄和性别? 如何查询所有男性员工的信息? 如何查询所有工资大于等于5000的员工信息? 如何查询所有工资在4000到6000之间的员工信息? 如何查询所有入职日期在2019年的员工信息? 如何查询所有职位为经理的员工信息? 如何查询所有在部门D1工作的员工信息? 如何查询所有在部门D1或D2工作的员工信息? 如何查询所有员工的工资总和? 如何查询所有员工的平均工资? 如何查询所有部门的员工总数? 如何查询所有部门的平均工资? 如何查询所有部门的最高工资? 如何查询所有部门的最低工资? 如何查询所有员工的最高工资? 如何查询所有员工的最低工资? 如何查询所有员工的工资排名?
1. 如何查询员工ID为1001的信息?
```
SELECT * FROM employees WHERE id = 1001;
```
2. 如何查询所有员工的姓名和工资?
```
SELECT name, salary FROM employees;
```
3. 如何查询所有员工的入职日期和所在部门?
```
SELECT hire_date, department FROM employees;
```
4. 如何查询所有员工的年龄和性别?
```
SELECT age, gender FROM employees;
```
5. 如何查询所有男性员工的信息?
```
SELECT * FROM employees WHERE gender = 'Male';
```
6. 如何查询所有工资大于等于5000的员工信息?
```
SELECT * FROM employees WHERE salary >= 5000;
```
7. 如何查询所有工资在4000到6000之间的员工信息?
```
SELECT * FROM employees WHERE salary BETWEEN 4000 AND 6000;
```
8. 如何查询所有入职日期在2019年的员工信息?
```
SELECT * FROM employees WHERE YEAR(hire_date) = 2019;
```
9. 如何查询所有职位为经理的员工信息?
```
SELECT * FROM employees WHERE title = 'Manager';
```
10. 如何查询所有在部门D1工作的员工信息?
```
SELECT * FROM employees WHERE department = 'D1';
```
11. 如何查询所有在部门D1或D2工作的员工信息?
```
SELECT * FROM employees WHERE department IN ('D1', 'D2');
```
12. 如何查询所有员工的工资总和?
```
SELECT SUM(salary) FROM employees;
```
13. 如何查询所有员工的平均工资?
```
SELECT AVG(salary) FROM employees;
```
14. 如何查询所有部门的员工总数?
```
SELECT department, COUNT(*) FROM employees GROUP BY department;
```
15. 如何查询所有部门的平均工资?
```
SELECT department, AVG(salary) FROM employees GROUP BY department;
```
16. 如何查询所有部门的最高工资?
```
SELECT department, MAX(salary) FROM employees GROUP BY department;
```
17. 如何查询所有部门的最低工资?
```
SELECT department, MIN(salary) FROM employees GROUP BY department;
```
18. 如何查询所有员工的最高工资?
```
SELECT MAX(salary) FROM employees;
```
19. 如何查询所有员工的最低工资?
```
SELECT MIN(salary) FROM employees;
```
20. 如何查询所有员工的工资排名?
```
SELECT id, name, salary, FIND_IN_SET(salary, (SELECT GROUP_CONCAT(DISTINCT salary ORDER BY salary DESC) FROM employees)) AS rank FROM employees ORDER BY rank;
```
阅读全文