第五次作业

USE mydb3;
 SELECT * FROM dept;
 SELECT * FROM emp;
#SELECT * FROM dept d,emp e
#WHERE d.dept1 = e.dept2;
 -- 1.找出销售部门中年纪最大的员工的姓名
 SELECT name FROM emp   WHERE age = (SELECT MAX(age) FROM emp) ORDER BY age desc;
 -- 2.求财务部门最低工资的员工姓名
  SELECT name FROM emp   WHERE age = (SELECT min(age) FROM emp) ORDER BY age desc;
 -- 3.列出每个部门收入总和高于9000的部门名称
SELECT d.dept_name, SUM(e.incoming) AS total_income
FROM dept d
INNER JOIN emp e ON d.dept1 = e.dept2
GROUP BY d.dept_name
HAVING SUM(e.incoming) > 9000;
 -- 4.求工资在7500到8500元之间,年龄最大的人的姓名及部门
 SELECT e.name, d.dept_name
FROM emp e
INNER JOIN dept d ON e.dept2 = d.dept1
WHERE e.incoming BETWEEN 7500 AND 8500
AND e.age = (
    SELECT MAX(age)
    FROM emp
    WHERE incoming BETWEEN 7500 AND 8500
);
 -- 5.找出销售部门收入最低的员工入职时间
 SELECT e.worktime_start
FROM emp e
INNER JOIN dept d ON e.dept2 = d.dept1
WHERE d.dept_name = '销售' AND e.incoming = (
    SELECT MIN(incoming)
    FROM emp
    WHERE dept2 = 102
);
 -- 6.财务部门收入超过2000元的员工姓名
 SELECT e.name
FROM emp e
INNER JOIN dept d ON e.dept2 = d.dept1
WHERE d.dept_name = '财务' AND e.incoming > 2000;
 -- 7.列出每个部门的平均收入及部门名称
 SELECT d.dept_name, AVG(e.incoming) AS avg_income
FROM dept d
INNER JOIN emp e ON d.dept1 = e.dept2
GROUP BY d.dept_name;
 -- 8.IT技术部入职员工的员工号
 SELECT e.sid
FROM emp e
INNER JOIN dept d ON e.dept2 = d.dept1
WHERE d.dept_name = 'IT技术';
 -- 9.财务部门的收入总和;
 SELECT SUM(e.incoming) AS total_income
FROM emp e
INNER JOIN dept d ON e.dept2 = d.dept1
WHERE d.dept_name = '财务';
 -- 10.找出哪个部门还没有员工入职;
 SELECT d.dept_name
FROM dept d
LEFT JOIN emp e ON d.dept1 = e.dept2
WHERE e.sid IS NULL;
 -- 11.列出部门员工收入大于7000的部门编号,部门名称;
 SELECT d.dept1, d.dept_name
FROM dept d
INNER JOIN emp e ON d.dept1 = e.dept2
GROUP BY d.dept1, d.dept_name
HAVING MAX(e.incoming) > 7000;
 -- 12.列出每一个部门的员工总收入及部门名称;
 SELECT d.dept_name, SUM(e.incoming) AS total_income
FROM dept d
INNER JOIN emp e ON d.dept1 = e.dept2
GROUP BY d.dept_name;
 -- 13.列出每一个部门中年纪最大的员工姓名,部门名称;
 SELECT e.name, d.dept_name
FROM emp e
INNER JOIN dept d ON e.dept2 = d.dept1
WHERE (e.dept2, e.age) IN (
    SELECT dept2, MAX(age)
    FROM emp
    GROUP BY dept2
);
 -- 14.求李四的收入及部门名称
 SELECT e.incoming, d.dept_name
FROM emp e
INNER JOIN dept d ON e.dept2 = d.dept1
WHERE e.name = '李四';
 -- 15.列出每个部门中收入最高的员工姓名,部门名称,收入,并按照收入降序
 SELECT e.name, d.dept_name, e.incoming
FROM emp e
INNER JOIN dept d ON e.dept2 = d.dept1
WHERE (e.dept2, e.incoming) IN (
    SELECT dept2, MAX(incoming)
    FROM emp
    GROUP BY dept2
)
ORDER BY e.incoming DESC;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值