-- 工资等级表
CREATE TABLE salarygrade (
grade INT PRIMARY KEY, -- 级别
losalary INT, -- 最低工资
hisalary INT -- 最高工资
);
-- 添加5个工资等级
INSERT INTO salarygrade(grade,losalary,hisalary) VALUES
(1,7000,12000),
(2,12010,14000),
(3,14010,20000),
(4,20010,30000),
(5,30010,99990);
SHOW TABLES;
-- 需求:
-- 1.查询所有员工信息。查询员工编号,员工姓名,工资,职务名称,职务描述
SELECT e.`id`,e.`ename`,e.`salary`,j.`jname`,j.`description` FROM emp e,job j WHERE e.`job_id`=j.`id`;
-- 2.查询员工编号,员工姓名,工资,职务名称,职务描述,部门名称,部门位置
SELECT e.`id`,e.`ename`,e.`salary`,j.`jname`,j.`description`,d.`dname`,d.`loc` FROM emp e,job j,dept d WHERE e.`job_id`=j.`id` AND e.`dept_id`=d.`id`
-- 3.查询员工姓名,工资,工资等级
SELECT e.`ename`,e.`salary`,t2.grade FROM emp e,salarygrade t2 WHERE e.`salary` BETWEEN t2.losalary AND t2.hisalary;
-- 4.查询员工姓名,工资,职务名称,职务描述,部门名称,部门位置,工资等级
SELECT e.`ename`,e.`salary`,j.`jname`,j.`description`,d.`dname`,d.`loc`,s.`grade`
FROM emp e,job j,dept d,salarygrade s
WHERE e.`job_id`=j.`id` AND e.`dept_id`=d.`id`AND e.`salary` BETWEEN s.`losalary` AND s.`hisalary`;
-- 5.查询出部门编号、部门名称、部门位置、部门人数
SELECT
t2.`id`,t2.`dname`,t2.`loc`,t1.total
FROM
(SELECT emp.`dept_id` ,COUNT(id) total FROM emp GROUP BY emp.`dept_id`) t1,
dept t2
WHERE
t1.dept_id=t2.`id`;
-- 6.查询所有员工的姓名及其直接上级的姓名,没有领导的员工也需要查询
SELECT
t1.`id`,t1.`ename`,t2.`id`,t2.`ename`
FROM
emp t1 LEFT JOIN emp t2
ON
t1.`mgr`=t2.`id`;