10-mysql DQL--查询练习

1 查询工资最低的员工信息
SELECT * FROM employees WHERE salary=(SELECT MIN(salary) FROM employees);

2 查询平均工资最低的部门信息
实现一(写复杂了):
SELECT AVG(salary) AS avgsalary,department_id FROM employees 
GROUP BY department_id ORDER BY avgsalary ASC LIMIT 1;

SELECT * FROM departments d
WHERE d.`department_id`=(
	SELECT department_id FROM (
	SELECT AVG(salary) AS avgsalary,department_id FROM employees 
	GROUP BY department_id ORDER BY avgsalary ASC LIMIT 1
	  ) AS temp
);
实现二:
SELECT department_id FROM employees e
GROUP BY e.`department_id` 
ORDER BY AVG(salary)
LIMIT 1;

SELECT * FROM departments d
WHERE d.`department_id`=(
	SELECT department_id FROM employees e
	GROUP BY e.`department_id` 
	ORDER BY AVG(salary)
	LIMIT 1
);

3 查询平均工资最低的部门信息和该部门的平均工资
查询平均工资最低的部门
SELECT department_id,MIN(salary) minsalary FROM employees e
GROUP BY department_id 
ORDER BY MIN(salary)
LIMIT 1;
#查询部门信息
SELECT d.*,t.minsalary
FROM departments d
INNER JOIN (
	SELECT department_id,MIN(salary) minsalary FROM employees e
	GROUP BY department_id 
	ORDER BY MIN(salary)
	LIMIT 1
) t
ON t.department_id= d.`department_id`;

4 查询平均工资最高的job信息
从employees中查询平均工资最高的job_id
SELECT job_id FROM employees e
GROUP BY job_id
ORDER BY MAX(salary) DESC
LIMIT 1;
#查job信息
SELECT * FROM jobs j
WHERE j.`job_id`=(
	SELECT job_id FROM employees e
	GROUP BY job_id
	ORDER BY MAX(salary) DESC
	LIMIT 1
);


5 查询平均工资高于公司平均工资的部门有哪些
查询公司平均工资
SELECT AVG(salary) FROM employees;
查询大于平均工资的部门id
SELECT department_id FROM employees 
GROUP BY department_id
HAVING AVG(salary)>(
	SELECT AVG(salary) FROM employees
);
result:
SELECT * FROM departments d
WHERE d.`department_id` IN (
	SELECT department_id FROM employees 
	GROUP BY department_id
	HAVING AVG(salary)>(
	SELECT AVG(salary) FROM employees
        )
);

6 查询出公司中所有manager的详细信息
查询所有的manager_id
SELECT DISTINCT manager_id FROM employees e
WHERE e.`manager_id` IS NOT NULL;

查询详细信息
SELECT * FROM employees e
WHERE e.`employee_id` IN (
	SELECT DISTINCT manager_id FROM employees e
	WHERE e.`manager_id` IS NOT NULL
);

7 查询各个部门中 最高工资中 最低的那个 部门的最低工资是多少
最高工资中 最低的那个 部门id
SELECT MAX(salary),department_id FROM employees
GROUP BY department_id
ORDER BY MAX(salary)  #默认升序排序 asc
LIMIT 1;

SELECT department_id FROM employees
GROUP BY department_id
ORDER BY MAX(salary)  #默认升序排序 asc
LIMIT 1;

#result  group by后面的筛选条件用 Having
SELECT MIN(salary) FROM employees e
GROUP BY department_id
HAVING e.`department_id`=(
	SELECT department_id FROM employees
	GROUP BY department_id
	ORDER BY MAX(salary)  #默认升序排序 asc
	LIMIT 1
)

8 查询平均工资最高的部门的manager的详细信息
查询平均工资最高的部门
SELECT MAX(salary),department_id FROM employees 
GROUP BY department_id
ORDER BY MAX(salary) DESC
LIMIT 1;

#max(salary) 直接放到 order by后面,select后面不用放,因为关心的是部门id
SELECT department_id FROM employees 
GROUP BY department_id
ORDER BY MAX(salary) DESC
LIMIT 1;

实现一:
获取manager_id,这里实现的有点傻,因为manager_id 从departments表可以直接拿到
SELECT DISTINCT e.`manager_id` FROM employees e
WHERE e.`department_id`=(
	SELECT department_id FROM employees 
	GROUP BY department_id
	ORDER BY MAX(salary) DESC
	LIMIT 1
) AND e.`manager_id` IS NOT NULL;

#manager 详细信息
SELECT * FROM employees 
WHERE employees.`employee_id`=(
  SELECT DISTINCT e.`manager_id` FROM employees e
  WHERE e.`department_id`=(
	SELECT department_id FROM employees 
	GROUP BY department_id
	ORDER BY MAX(salary) DESC
	LIMIT 1
  ) AND e.`manager_id` IS NOT NULL
);

实现二(推荐):
SELECT * FROM employees e
INNER JOIN departments d
ON e.`employee_id`=d.`manager_id`
WHERE d.`department_id`=(
	SELECT department_id FROM employees 
	GROUP BY department_id
	ORDER BY MAX(salary) DESC
	LIMIT 1
);

你可能感兴趣的:(Mysql)