1. 程式人生 > >mac終端操作資料庫--(4)一道綜合題

mac終端操作資料庫--(4)一道綜合題

mac終端操作資料庫–(4)一道綜合題

1. 題目

職員表 在這裡插入圖片描述

部門表 在這裡插入圖片描述

2. 建立資料庫和表,插入資料

//登入MySQL
/usr/local/mysql/bin/mysql -u root -p

//建立資料庫
1.create database zhouyu03;
2.use zhouyu03;
3.select database();

//建立表
CREATE TABLE emp(
	empno INT,
	ename VARCHAR(50),
	job VARCHAR(50),
	mgr INT,
	hiredate DATE,
	sal DECIMAL(7,2),
	comm DECIMAL(7,2),
	deptno INT
);

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-04-19',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-05-23',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,'1982-01-23',1300,NULL,10);
INSERT INTO emp VALUES(7981,'MILLER','CLERK',7788,'1992-01-23',2600,500,20);


CREATE TABLE dept(
	deptno INT,
	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');

在這裡插入圖片描述

3. 相關問題與解答

//基本查詢
//1.查詢所有員工資訊
select *from emp;
//2.薪資大於等於1000並且小於等於2000的員工資訊
select *from emp e where e.sal >= 1000 and e.sal <= 2000;
select *from emp e where e.sal between 1000 and 2000;
select *from emp e where e.sal between 1000 and 2000 order by desc;
//3.從員工表中查詢出所有部門的編號
select distinct e.deptno from emp e;
select distinct e.deptno as "所有部門編號" from emp e;
select distinct e.deptno "所有部門編號" from emp e;
//4.查詢出名字以A開頭的員工資訊
select *from emp e where e.ename like "A%";
//5.查詢出名字第二個字母是L的員工的資訊
select *from emp e where e.ename like "_L%";
//6.查詢出沒有獎金的員工資訊
select *from emp e where e.comm is null or e.comm = 0;
//7.所有員工的平均工資
select AVG(e.sal) from emp e;
//8.所有員工的工資總和
select SUM(e.sal) from emp e;
//9.所有員工的數量
select COUNT(*) from emp e;
//10.最高工資
select MAX(e.sal) from emp e;
//11.最少工資
select MIN(e.sal) from emp e;
//12.最高工資的員工資訊
select *from emp e where e.sal = (select MAX(e.sal) from emp e);

//分組查詢
//13.每個部門的平均工資
select *from dept d;
select d.deptno from dept d;
select d.dname,AVG(e.sal) from emp e, dept d where d.deptno = e.deptno group by d.dname;
select d.dname "部門名稱",AVG(e.sal) "部門平均工資" from emp e, dept d where d.deptno = e.deptno group by d.dname;

//子查詢

//單行子查詢(>, <, >=, <=, =, <>)
//14.查詢出高於10號部門的平均工資的員工資訊: 1.10號部門的平均工資 2.高於10號部門的平均工資的員工資訊
select AVG(e.sal) from emp e where e.deptno = 10;
select *from emp e where e.sal > (select AVG(e.sal) from emp e where e.deptno = 10);

//多行子查詢(in, not in, any, all)
//15.查詢出比20號部門任何員工工資都高的員工資訊: 
//1.20號部門最高的工資 2.查詢出比20號部門任何員工工資都高的員工資訊
select MAX(e.sal) from emp e where e.deptno = 20;
select *from emp e where e.sal > (select MAX(e.sal) from emp e where e.deptno = 20);
//2.20號部門的所有工資資訊 2.得出結果
select e.sal from emp e where e.deptno = 20;
select *from emp where sal > ALL(select sal from emp where deptno = 20);


//多列子查詢(in)
//16.和10號部門同名同工作的員工資訊: 1.10號部門員工姓名,工作資訊 2.結果
select ename,job from emp where deptno = 10;
select *from emp where (ename,job) in (select ename,job from emp where deptno = 10) and deptno != 10;

//select接子查詢
//17.獲取員工的名字和部門的名字: 1.獲取員工名字和部門id 2.獲取員工部門名字
select e.ename,e.deptno from emp e;
select e.ename,(select d.dname from dept d where d.deptno = e.deptno) from emp e;
select e.ename,d.dname from emp e,dept d where e.deptno = d.deptno;


//where接子查詢
//18.薪資高於10號部門平均工資的員工資訊
同14題

//having後面接子查詢
//19.有哪些部門的平均工資高於30號部門的平均工資: 1.30號部門的平均工資 2.所有部門的平均工資 3.對比得出結果
select AVG(sal) from emp where deptno = 30;
select deptno,AVG(sal) from emp group by deptno;
select deptno,AVG(sal) from emp group by deptno having AVG(sal) > (select AVG(sal) from emp where deptno = 30);

//總和查詢
//20.查詢高於本部門平均工資的員工資訊: 1.獲取所有部門的平均工資 2.得出結果
select e.deptno,AVG(e.sal) from emp e group by e.deptno;
select *from emp e1 where e1.sal > (select AVG(e2.sal) from emp e2 where e1.deptno = e2.deptno group by e2.deptno);

//21.列出達拉斯加工作的人中,比紐約的平均工資高的員工資訊: 1.找出紐約城市 2.紐約的平均工資 3.達拉斯加工作的員工 4.計算出結果
select deptno from dept where loc = "NEW YORK";
select AVG(e.sal) from emp e where e.deptno = (select d.deptno from dept d where d.loc = "NEW YORK");
select d.deptno from dept d where d.loc = "DALLAS";
select *from emp e1 where e1.deptno = (select d1.deptno from dept d1 where d1.loc = "DALLAS") and e1.sal > (select AVG(e2.sal) from emp e2 where e2.deptno = (select d2.deptno from dept d2 where d2.loc = "NEW YORK"));


//22.查詢出各個部門薪資最高的員工資訊 1.各個部門的最高薪資 2.本部門最高子薪資員工資訊
select MAX(e1.sal) from emp e1 group by e1.deptno;
select *from emp e2 where e2.sal = (select MAX(e1.sal) from emp e1 where e2.deptno = e1.deptno group by e1.deptno);