MySQL数据库之综合练习(一)

发布时间:2024年01月20日

首先我们先创建一个数据库dept_emp

4def5f51b2cf4ee3851286c449ba4780.png

接下来我们创建dept表,emp表,salgrade表

USE dept_emp; ? ?

CREATE TABLE dept (
?? ?deptno INT(2) NOT NULL COMMENT '部门编号',
?? ?dname VARCHAR (15) COMMENT '部门名称',
?? ?loc VARCHAR (20) COMMENT '地理位置'?
);

-- 添加主键
ALTER TABLE dept ADD PRIMARY KEY (deptno);

-- 添加数据
INSERT INTO dept (deptno,dname,loc)VALUES (10,'财务部','高新四路');
INSERT INTO dept (deptno,dname,loc)VALUES (20,'人事部','科技二路');
INSERT INTO dept (deptno,dname,loc)VALUES (30,'销售部','长安区');
INSERT INTO dept (deptno,dname,loc)VALUES (40,'运输部','雁塔区');

------------------------------------------------------------------
CREATE TABLE emp(
?? ?empno INT(4) NOT NULL COMMENT '员工编号',
?? ?ename VARCHAR(10) COMMENT '员工名字',
?? ?job VARCHAR(10) COMMENT '职位',
?? ?mgr INT(4) COMMENT '上司',
?? ?hiredate DATE COMMENT '入职时间',
?? ?sal INT(7) COMMENT '基本工资',
?? ?comm INT(7) COMMENT '补贴',
?? ?deptno INT(2) COMMENT '所属部门编号'
);

-- 添加主键
ALTER TABLE emp ADD PRIMARY KEY (empno);

-- 添加外键约束
ALTER TABLE emp ADD CONSTRAINT f_ed_key
FOREIGN KEY (deptno)?
REFERENCES dept(deptno);

INSERT INTO `emp` VALUES('7369','张倩','办事员','7902','2002-12-17','820',NULL,'20');
INSERT INTO `emp` VALUES('7499','刘博','售货员','7698','1992-02-20','1900','300','30');
INSERT INTO `emp` VALUES('7521','李兴','售货员','7698','1995-07-22','1250','500','30');
INSERT INTO `emp` VALUES('7566','李雷','人事部长','7839','1991-04-02','975',NULL,'20');
INSERT INTO `emp` VALUES('7654','刘浩','售货员','7698','1991-09-28','1250','1400','30');
INSERT INTO `emp` VALUES('7698','刘涛','销售部长','7839','1997-05-01','2850',NULL,'30');
INSERT INTO `emp` VALUES('7782','华仔','人事部长','7839','1995-06-09','2450',NULL,'10');
INSERT INTO `emp` VALUES('7788','张飞','人事专员','7566','1998-04-19','3000',NULL,'20');
INSERT INTO `emp` VALUES('7839','马晓云','董事长',NULL,'1991-11-17','5000',NULL,'10');
INSERT INTO `emp` VALUES('7844','马琪','售货员','7698','1996-09-08','1500','0','30');
INSERT INTO `emp` VALUES('7876','李涵','办事员','7788','1997-05-23','1100',NULL,'20');
INSERT INTO `emp` VALUES('7900','李小涵','销售员','7698','1993-2-13','950',NULL,'30');
INSERT INTO `emp` VALUES('7902','张三','人事组长','7566','1992-10-08','3000',NULL,'20');
INSERT INTO `emp` VALUES('7934','张三丰','人事长','7782','1997-06-23','1300',NULL,'10');

------------------------------------------------------------------------
??
CREATE TABLE salgrade(
grade INT (10) COMMENT '工资等级',
losal INT (10) COMMENT '最低限额',
hisal INT (10) COMMENT '最高限额'
)

INSERT INTO salgrade (grade, losal, hisal)VALUES (1, 700, 1200);
INSERT INTO salgrade (grade, losal, hisal)VALUES (2, 1201, 1400);
INSERT INTO salgrade (grade, losal, hisal)VALUES (3, 1401, 2000);
INSERT INTO salgrade (grade, losal, hisal)VALUES (4, 2001, 3000);
INSERT INTO salgrade (grade, losal, hisal)VALUES (5, 3001, 9999);

单表查询
1.选择部门30中的所有员工

mysql> select *from emp where deptno=30;

ae9aca503ed047739141e7b7558ab157.png

2.列出所有办事员的姓名,编号和部门编号

mysql> select ename,empno,deptno from emp where job='办事员';

a4ac1a10af9f4f439211f717b54d720c.png

3.找出佣金高于薪金的员工

mysql> select ename,sal,comm from emp where comm>sal;

d93cda085dbf45c2b863d70c1a6373d7.png

4.找出没有佣金的员工

mysql> select ename,sal,comm from emp where comm is null or comm=0;

a4c1b595094e4d00b0613a15a5506c16.png

5.找出佣金高于薪金的60%的员工

mysql> select ename,sal,comm from emp where comm > sal*0.6;

f4cbdbd53b5f4758889ad278f3583e06.png

6.找出部门10中所有人事部长和部门20中所有办事员的详细资料

mysql> select *from emp where(deptno=10 and job='人事部长') or (deptno=20 and job='办事员');

dce9a5bdee134bc8a83854b6c8a3dbd6.png

7.找出收取佣金的员工

mysql> select *from emp where comm>0 and comm is not null;

3238db1e0b274c2fab277e4f86b04855.png

8.找出不收取佣金或收取的佣金低于100的员工

mysql> select *from emp where comm is null or comm<100;

d7f94cf12ab3490a962d3c12fdb89c91.png

9.找出姓张的员工的信息

mysql> select *from emp where ename like '%张%';

9c50599b82454b4382fd26047cd2f49d.png

10.显示员工的姓名和受雇日期,新的员工排在最前面

mysql> select ename,hiredate from emp order by hiredate desc;

f24b0f61b8c4418680a5368216f1518d.png

11.按工作的降序排序,若工作相同则按薪金排序

mysql> select ename,hiredate from emp order by job,sal;

667a321f66ba400e8fadd55bedae1598.png

12.查出不姓刘的员工的信息

mysql> select *from emp where ename not like "刘%";

3eca34462474409a88e1927cfcfa6185.png

13.取出姓李的员工

mysql> select *from emp where ename like "李%";

3616ce63476447a38344a4154bc1bc40.png

多表查询
1. 查询出每一位雇员的编号、姓名、职位、部门名称、位置

mysql> select e.empno,e.ename,e.job,d.dname,d.loc from emp e,dept d where e.deptno=d.deptno;

5235005450444c6db5c3732ff89254b7.png

2.要求查询出每一位雇员的姓名、职位、领导的姓名

mysql> select e.ename,e.job,m.ename from emp e, emp m where e.mgr=m.empno;

f84aa5a5085f442181cd73bdada42a13.png

3.查询出每个雇员的编号、姓名、基本工资、职位、领导的姓名、部门名称及位置。

mysql> select e.empno,e.ename,e.sal,e.job,e.mgr,d.dname,d.loc from emp e,emp m,dept d where e.mgr=m.empno and e.deptno=d.deptno;

6c13b148f9f5497f8c6b41352da32c6c.png

4.列出在部门"销售部"工作的员工的姓名,假定不知道销售部的部门编号。

mysql> select e.ename from emp e,dept d where e.deptno=d.deptno and d.dname="销售部";

7211f304b6a14ec7b15069500dcdf4af.png

5.列出与"李兴"从事相同工作的所有员工。

mysql> select e.* from emp e where e.job=(select e.job from emp e where e.ename="李兴");
0af83f9e5bfe46f88d8b07c564e2d3ba.png

6.列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金。?

mysql> select e.ename,e.sal from emp e where e.sal>all(select e.sal from emp e where deptno='30');
74b64174ccd541a5a33f860af0a11fbe.png

7.列出受雇日期早于其直接上级的所有员工。

mysql> select e.ename,e.hiredate from emp e join emp m on e.mgr=m.empno where e.hiredate <m.hiredate;
3cd1e79b9e154c79820e47cc789f1290.png

分组函数查询
1.按照部门编号分组,求出每个部的人数,平均工资

mysql> select deptno,count(empno),avg(sal) from emp group by deptno;

fe4d03212a18466f9580fe78898a87e3.png

2.按照职位分组,求出每个职位的最高和最低工资

mysql> select job,max(sal),min(sal) from emp group by job;
dbec32cf28724a92824393a745b56ec4.png

3.计算出每个各职位的平均工资

mysql> select job,avg(sal) from emp group by job;
0aab094946ae4c849813f114cf156035.png

4.查询出每个部门的名称、部门的人数、平均工资

mysql> select d.dname,count(e.empno),avg(e.sal) from dept d,emp e where d.deptno=e.deptno group by d.dname;

c84c9021a2f24424bf15d8234ad9e96e.png

5.要求统计出每个部门的详细信息,并且要求这些部门的平均工资高于2000;

mysql> select d.deptno,d.dname,d.loc,count(*),avg(sal) from dept d join emp e on d.deptno=e.deptno group by d.deptno,d.dname,d.loc having avg(sal)>2000

58936c5985234af4b8e8808883c9ec91.png

6.要求查询出工资比华仔还要高的全部雇员信息

mysql> select *from emp where sal>(select sal from emp where ename='华仔');

b8b7a85613954adda99217ed983c20d3.png

7.要求查询出高于公司平均工资的全部雇员信息

mysql> select *from emp where sal>(select avg(sal) from emp);

3170121903634fa7aac0ae95af692e2f.png

8.查询出每个部门的编号、名称、位置、部门人数、平均工资

mysql> select d.deptno,d.dname,d.loc,count(*),avg(sal) from dept d join emp e on d.deptno=e.deptno group by d.deptno,d.dname,d.loc;

3312ba1f456a4465b723948c3ceb14e6.png

9.列出至少有一个员工的所有部门。?

mysql> select deptno,dname,loc from dept where deptno in(select deptno from emp group by deptno having count(*)>0);

fbdc3fd7fe3348dba0b49538651862b2.png

等级查询
1.查询每个工资等级各有多少员工

mysql> select s.grade,count(*) from emp e join salgrade s on e.sal between s.losal and s.hisal group by s.grade;
800f80c0ac5e40318e77e3d57ca6bc53.png

2.查询部门中(所有人)的平均工资等级
mysql> select e.deptno,avg(s.grade) from emp e join salgrade s on e.sal between s.losal and s.hisal group by?e.deptno;

0e4f76b9e179409f9c71c956183a1521.png

?

文章来源:https://blog.csdn.net/weixin_72191146/article/details/135715448
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。