MySQL入門練習(二)
本篇是上一篇的強化與擴充套件篇,同樣材料來自於傳智播客。
一、表格資訊
1、部門表:由部門號、部門名稱、部門地址組成:
CREATE TABLE dept( deptno int PRIMARY KEY, dname VARCHAR(14) , loc VARCHAR(13) ) ; INSERT INTO DEPT VALUES (10,'ACCOUNTING','NEW YORK'); INSERT INTO DEPT VALUES (20,'RESEARCH','DALLAS'); INSERT INTO DEPT VALUES (30,'SALES','CHICAGO'); INSERT INTO DEPT VALUES (40,'OPERATIONS','BOSTON');
2、員工表:由員工編號、姓名、工作、直屬領導、入職日期、工資、獎金、部門號組成:
CREATE TABLE emp( empno int PRIMARY KEY, ename VARCHAR(10), job VARCHAR(9), mgr int, hiredate DATE, sal double, comm double, deptno int , foreign key emp(deptno) REFERENCES dept(deptno) ); INSERT INTO EMP VALUES(7369,'SMITH','CLERK',7902,"1980-12-17",800,NULL,20); INSERT INTO EMP VALUES(7499,'ALLEN','SALESMAN',7698,'1981-02-20',1600,300,30); INSERT INTO EMP VALUES(7521,'WARD','SALESMAN',7698,'1981-02-22',1250,500,30); INSERT INTO EMP VALUES(7566,'JONES','MANAGER',7839,'1981-04-02',2975,NULL,20); INSERT INTO EMP VALUES(7654,'MARTIN','SALESMAN',7698,'1981-09-28',1250,1400,30); INSERT INTO EMP VALUES(7698,'BLAKE','MANAGER',7839,'1981-05-01',2850,NULL,30); INSERT INTO EMP VALUES(7782,'CLARK','MANAGER',7839,'1981-06-09',2450,NULL,10); INSERT INTO EMP VALUES(7788,'SCOTT','ANALYST',7566,'1987-07-03',3000,NULL,20); INSERT INTO EMP VALUES(7839,'KING','PRESIDENT',NULL,'1981-11-17',5000,NULL,10); INSERT INTO EMP VALUES(7844,'TURNER','SALESMAN',7698,'1981-09-08',1500,0,30); INSERT INTO EMP VALUES(7876,'ADAMS','CLERK',7788,'1987-07-13',1100,NULL,20); INSERT INTO EMP VALUES(7900,'JAMES','CLERK',7698,'1981-12-03',950,NULL,30); INSERT INTO EMP VALUES(7902,'FORD','ANALYST',7566,'1981-12-03',3000,NULL,20); INSERT INTO EMP VALUES(7934,'MILLER','CLERK',7782,'1981-01-23',1300,NULL,10);
3、薪資等級表:由等級、最低工資、最高工資組成:
CREATE TABLE salgrade(
grade int,
losal double,
hisal double
);
INSERT INTO SALGRADE VALUES (1,700,1200);
INSERT INTO SALGRADE VALUES (2,1201,1400);
INSERT INTO SALGRADE VALUES (3,1401,2000);
INSERT INTO SALGRADE VALUES (4,2001,3000);
INSERT INTO SALGRADE VALUES (5,3001,9999);
二、表格內容輸出
輸出3個表格內容:
select * from emp;
select * from dept;
select * from salgrade;
1、員工表:
2、部門表:
3、薪資等級表:
三、基本練習
1、查詢部門30中員工的詳細資訊。
2、找出從事clerk工作的員工的編號、姓名、部門號。
3、檢索出獎金多於基本工資的員工資訊。
4、檢索出獎金多於基本工資60%的員工資訊。
5、找出10部門的經理、20部門的職員 的員工資訊。
6、找出10部門的經理、20部門的職員 或者既不是經理也不是職員但是工資高於2000元的員工資訊。
7、找出獲得獎金的員工的工作。
8、找出獎金少於100或者沒有獲得獎金的員工的資訊。
9、找出姓名以A、B、S開始的員工資訊。
10、找到名字長度為4個字元的員工資訊。
11、名字中不包含R字元的員工資訊。
12、返回員工的詳細資訊並按姓名排序。
13、返回員工的資訊並按工作降序工資升序排列。
14、計算員工的日薪(按30天)。
15、找出姓名中包含A的員工資訊。
四、基本練習答案
1、select * from emp where deptno=30;
2、select empno,ename,deptno from emp where job='clerk';
3、select * from emp where comm > sal;
4、select * from emp where comm > sal*0.6;
5、select * from emp where (job='manager' and deptno=10) or (job='clerk' and deptno=20);
6、select * from emp where (job='manager' and deptno=10) or (job='clerk' and deptno=20) or (job not in('manager','clerk') and sal>2000);
7、select job from emp where comm is not null;
8、select * from emp where comm is null or comm<100;
9、select * from emp where ename like 'A%' or ename like 'B%' or ename like 'S%';
10、select * from emp where length(ename)=4;
11、select * from emp where ename not like '%R%';
12、select * from emp order by ename;
13、select * from emp order by job desc,sal asc;
14、select ename,sal/30 from emp;
15、select * from emp where ename like '%A%';
四、擴充套件練習
1、返回擁有員工的部門名、部門號;
2、工資水平多於smith的員工資訊;
3、返回員工和所屬經理的姓名;
4、返回僱員的僱傭日期早於其經理僱傭日期的員工及其經理姓名;
5、返回員工姓名及其所在的部門名稱;
6、返回從事clerk工作的員工姓名和所在部門名稱;
7、返回部門號及其本部門的最低工資;
8、返回銷售部(sales)所有員工的姓名;
9、返回工資水平多於平均工資的員工;
10、返回與SCOTT從事相同工作的員工;
11、返回與30部門員工工資水平相同的員工姓名與工資;
12、返回工資高於30部門所有員工工資水平的員工資訊;
13、返回部門號、部門名、部門所在位置及其每個部門的員工總數;
14、返回員工的姓名、所在部門名及其工資;
15、返回員工的詳細資訊。(包括部門名)了;
16、返回員工工作及其從事此工作的最低工資;
17、返回不同部門經理的最低工資;
18、計算出員工的年薪,並且以年薪排序;
19、返回工資處於第四級別的員工的姓名;
20、返回工資為二等級的職員名字、部門所在地、和二等級的最低工資和最高工資;
五、擴充套件練習答案
1、select d.dname,d.deptno from dept d,emp e where d.deptno=e.deptno group by d.deptno;
2、select * from emp where sal > (select sal from emp where ename ='smith');
3、select e.ename,em.ename from emp e,emp em where e.mgr=em.empno and em.job='manager';
4、select e.ename,em.ename from emp e,emp em where e.mgr = em.empno and em.job = 'manager' and e.hiredate < em.hiredate;
5、select e.ename,d.dname from emp e,dept d where e.deptno=d.deptno;
6、select e.ename,d.dname from emp e,dept d where e.deptno=d.deptno and e.job='clerk';
7、select d.deptno,min(e.sal) from dept d,emp e where d.deptno=e.deptno group by e.deptno;
8、select e.ename from emp e,dept d where e.deptno=d.deptno and d.dname='sales';
9、select * from emp e where e.sal > (select avg(sal) from emp);
10、select * from emp e where e.job =(select job from emp where ename='scott');
11、select e.ename,e.sal from emp e where e.sal in(select sal from emp where deptno=30);
12、select * from emp where sal > (select max(sal) from emp where deptno=30);
13、select d.deptno,d.dname,d.loc,count(*) from dept d,emp e where e.deptno=d.deptno group by d.deptno;
14、select e.ename,d.dname,e.sal from dept d,emp e where e.deptno=d.deptno;
15、select e.*,d.dname from emp e,dept d where e.deptno=d.deptno;
16、select e.job,min(e.sal) from emp e,dept d where e.deptno=d.deptno group by e.job;
17、select min(e.sal) from dept d,emp e where d.deptno = e.deptno and e.job = 'MANAGER' group by d.deptno ;
18、select ename,(sal+ifnull(0,comm))*12 sum from emp order by sum;
19、select e.ename,e.sal from emp e,salgrade s where e.sal between s.losal and s.hisal and s.grade=4;
20、select e.ename,d.loc,s.losal,s.hisal from emp e,dept d,salgrade s where e.deptno=d.deptno and e.sal between s.losal and s.hisal and s.grade=2;