mysql练习题

Posted 星辰安安

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql练习题相关的知识,希望对你有一定的参考价值。

练习题一

DROP TABLE IF EXISTS  `emp`;
CREATE TABLE `emp` (
  `EMPNO` int(4) NOT NULL,
  `ENAME` varchar(10) DEFAULT NULL,
  `JOB` varchar(9) DEFAULT NULL,
  `MGR` varchar(10) DEFAULT NULL,
  `HIREDATE` date DEFAULT NULL,
  `SAL` int(7) DEFAULT NULL,
  `COMM` int(7) DEFAULT NULL,
  `DEPTNO` int(2) DEFAULT NULL,
  PRIMARY KEY (`EMPNO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
字段中文名字依次是:工号,姓名,工作岗位,部门经理,受雇日期,薪金,奖金,部门编号
insert into `emp`(`EMPNO`,`ENAME`,`JOB`,`MGR`,`HIREDATE`,`SAL`,`COMM`,`DEPTNO`) values
('7369','SMITH','CLERK','7902','1980-12-17','800',null,'20'),
('7499','ALLEN','SALESMAN','7698','1981-02-20','1600','300','30'),
('7521','WARD','SALESMAN','7698','1981-02-22','1250','500','30'),
('7566','JONES','MANAGER','7839','1981-04-02','2975',null,'20'),
('7654','MARTIN','SALESMAN','7698','1981-09-28','1250','1400','30'),
('7698','BLAKE','MANAGER','7839','1981-05-01','2850',null,'30'),
('7782','CLARK','MANAGER','7839','1981-06-09','2450',null,'10'),
('7788','SCOTT','ANALYST','7566','1987-04-19','3000',null,'20'),
('7839','KING','PRESIDENT',null,'1981-11-17','5000',null,'10'),
('7844','TURNER','SALESMAN','7698','1981-09-08','1500','0','30'),
('7876','ADAMS','CLERK','7788','1987-05-23','1100',null,'20'),
('7900','JAMES','CLERK','7698','1981-12-03','950',null,'30'),
('7902','FORD','ANALYST','7566','1981-12-03','3000',null,'20'),
('7934','MILLER','CLERK','7782','1982-01-23','1300',null,'10');

DROP TABLE IF EXISTS  `dept`;
CREATE TABLE `dept` (
  `DEPTNO` int(2) NOT NULL,
  `DNAME` varchar(14) DEFAULT NULL,
  `LOC` varchar(13) DEFAULT NULL,
  PRIMARY KEY (`DEPTNO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

insert into `dept`(`DEPTNO`,`DNAME`,`LOC`) values
('10','ACCOUNTING','NEW YORK'),
('20','RESEARCH','DALLAS'),
('30','SALES','CHICAGO'),
('40','OPERATIONS','BOSTON');

1. 列出至少有4个员工的所有部门编号和名称。
-- select deptno,count(*) as sum from emp group by deptno having sum>=4;
-- select dept.dname,dept.deptno from (select deptno,count(*) as sum from emp group by deptno having sum>=4) as d
-- left JOIN
-- dept on d.deptno=dept.deptno;
2. 列出薪金比“SMITH”多的所有员工。
-- select * from emp where sal>(select sal from emp where ename='SMITH');
-- 3. 列出所有员工的姓名及其直接上级的姓名。 
-- select e1.ename,e2.ename from emp as e1
-- left join emp as e2
-- on e1.mgr=e2.empno
4. 列出受雇日期早于其直接上级的所有员工。
-- select e1.ename,e2.ename from emp as e1
-- left join emp as e2
-- on e1.mgr=e2.empno
-- WHERE e1.HIREDATE<e2.HIREDATE;
5. 列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门。
-- select * from dept
-- left join emp
-- on dept.deptno=emp.deptno;
6. 列出所有“CLERK”(办事员)的姓名及其部门名称。 
-- select * from (select * from emp WHERE job='CLERK')as e
-- LEFT JOIN dept 
-- on e.deptno=dept.deptno;
7. 列出最低薪金大于1500的各种工作。
-- select job,min(sal)as m from emp group by job having m>=1500; 
8. 列出在部门“SALES”(销售部)工作的员工的姓名,假定不知道销售部的部门编号
-- select * from dept where dname='SALES';
-- select * from (select * from dept where dname='SALES')as d
-- LEFT JOIN emp 
-- on d.deptno=emp.deptno;
9. 列出薪金高于公司平均薪金的所有员工。 
-- select * from emp where sal>(select avg(sal) from emp);
10.列出与“SCOTT”从事相同工作的所有员工。 
-- select * from emp where job=(select job from emp where ename='SCOTT') and ename<>'SCOTT';
11.列出薪金,等于部门30中员工的薪金的所有员工,的姓名和薪金。 
-- select DISTINCT sal from emp where deptno=30;
-- SELECT * from emp where deptno<>30 and sal in(select DISTINCT sal from emp where deptno=30);
12.列出薪金高于在部门30工作的所有员工的薪金,的员工姓名和薪金。
-- select max(sal) from emp where deptno=30;
-- select * from emp where sal>(select max(sal) from emp where deptno=30);
13.列出在每个部门工作的员工数量、平均工资和平均服务期限。 
-- select count(*),avg(sal),ROUND(avg(DATEDIFF(CURRENT_DATE(),HIREDATE)))/365 from emp GROUP BY deptno;
14.列出所有员工的姓名、部门名称和工资。 
-- select ename,dname,sal +(case when comm is null then 0 else comm end)   from emp
-- left join dept
-- on emp.deptno=dept.deptno;
15.列出所有部门的详细信息和部门人数。 
-- select * from dept
-- left join 
-- (select deptno,count(*) from emp GROUP BY deptno) as d
-- on d.deptno=dept.deptno;
16.列出各种工作的最低工资。 
-- select job,min(sal +(case when comm is null then 0 else comm end)) from emp group by job;
17.列出各个部门的MANAGER(经理)的最低薪金。 
-- select min(sal) from emp where job='MANAGER' group by deptno;
18.列出所有员工的年工资,按年薪从低到高排序。
-- select (sal +(case when comm is null then 0 else comm end))*12 as s from emp order by s
思考: 列出每个部门薪水前两名最高的人员名称以及薪水
-- select * from emp as  e1
-- where 2>(select count(*) from emp as e2 where e1.deptno=e2.deptno and e1.sal<e2.sal);

练习题二

create table test(
 	datetime date,
	sum int
 )ENGINE=innoDB;
 insert into test(datetime,sum) values('2018-6-1','10');
 insert into test(datetime,sum) values('2018-6-2','11');
 insert into test(datetime,sum) values('2018-6-3','11');
 insert into test(datetime,sum) values('2018-6-4','12');
 insert into test(datetime,sum) values('2018-6-5','14');
 insert into test(datetime,sum) values('2018-6-6','15');
 insert into test(datetime,sum) values('2018-6-7','13');
 insert into test(datetime,sum) values('2018-6-8','37');
 insert into test(datetime,sum) values('2018-6-9','18');
 insert into test(datetime,sum) values('2018-6-10','19');
 insert into test(datetime,sum) values('2018-6-11','10');
 insert into test(datetime,sum) values('2018-6-12','11');
 insert into test(datetime,sum) values('2018-6-13','11');
 insert into test(datetime,sum) values('2018-6-14','12');
 
-- 最后结果:
-- 2018-06-01~2018-06-07 12
-- 2018-06-08~2018-06-14 15
-- DATE_ADD(OrderDate,INTERVAL 2 DAY) 
-- select * from test;
-- 1.确定时间的开始'2018-06-01'
select DATEDIFF(datetime,'2018-06-01') from test;
-- 2.分组(时间相减的结果/7,向下取整)
select FLOOR(DATEDIFF(datetime,'2018-06-01')/7) from test;
-- 3.划分时间间隔
select FLOOR(DATEDIFF(datetime,'2018-06-01')/7)*7 from test;
-- 4.确定每个星期的初始时间
select DATE_ADD('2018-06-01',INTERVAL FLOOR(DATEDIFF(datetime,'2018-06-01')/7)*7 day) from test;
-- 4.确定每个星期的结束时间
select FLOOR(DATEDIFF(datetime,'2018-06-01')/7)*7+6 from test;
select DATE_ADD('2018-06-01',INTERVAL FLOOR(DATEDIFF(datetime,'2018-06-01')/7)*7+6 day) from test;
-- 拼接时间
select CONCAT(
DATE_ADD('2018-06-01',INTERVAL FLOOR(DATEDIFF(datetime,'2018-06-01')/7)*7 day),'~',
DATE_ADD('2018-06-01',INTERVAL FLOOR(DATEDIFF(datetime,'2018-06-01')/7)*7+6 day)) as time,FLOOR(avg(sum)) from test
group by time;

以上是关于mysql练习题的主要内容,如果未能解决你的问题,请参考以下文章

MySQL之多表查询练习

MySQL之多表查询练习

Oracle的sql语句上级练习和答案

02Hive数据仓库——SQL员工练习题

018.查询练习50题(sql实例)

《sql语句练习1》