-- 1、部门表
CREATE TABLE DEPT (
DEPTNO INT ( 2 ) NOT NULL,
DNAME VARCHAR ( 14 ),
LOC VARCHAR ( 13 ),
PRIMARY KEY ( DEPTNO ) );
-- 2、员工表
CREATE TABLE EMP (
EMPNO INT ( 4 ) NOT NULL,
ENAME VARCHAR ( 10 ),
JOB VARCHAR ( 9 ),
MGR INT ( 4 ),
HIREDATE DATE DEFAULT NULL,
SAL DOUBLE ( 7, 2 ),
COMM DOUBLE ( 7, 2 ),
PRIMARY KEY ( EMPNO ),
DEPTNO INT ( 2 )
-- 3、薪水等级表
CREATE TABLE SALGRADE (
GRADE INT,
LOSAL INT,
HISAL INT );
);
-- 1、插入部门表数据
INSERT INTO DEPT ( DEPTNO, DNAME, LOC ) VALUES ( 10, 'ACCOUNTING', 'NEW YORK');
INSERT INTO DEPT ( DEPTNO, DNAME, LOC ) VALUES ( 20, 'RESEARCH', 'DALLAS');
INSERT INTO DEPT ( DEPTNO, DNAME, LOC ) VALUES ( 30, 'SALES', 'CHICAGO');
INSERT INTO DEPT ( DEPTNO, DNAME, LOC ) VALUES ( 40, 'OPERATIONS', 'BOSTON');
commit;
-- 2、插入员工表数据
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,DEPTNO ) VALUES ( 7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, NULL, 20);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,DEPTNO ) VALUES ( 7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600, 300, 30);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,DEPTNO ) VALUES ( 7521, 'WARD', 'SALESMAN', 7698, '1981-02-22'
, 1250, 500, 30);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,DEPTNO ) VALUES ( 7566, 'JONES', 'MANAGER', 7839, '1981-04-02'
, 2975, NULL, 20);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,DEPTNO ) VALUES ( 7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28'
, 1250, 1400, 30);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,DEPTNO ) VALUES ( 7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01'
, 2850, NULL, 30);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,DEPTNO ) VALUES ( 7782, 'CLARK', 'MANAGER', 7839, '1981-06-09'
, 2450, NULL, 10);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,DEPTNO ) VALUES ( 7788, 'SCOTT', 'ANALYST', 7566, '1987-04-19'
, 3000, NULL, 20);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,DEPTNO ) VALUES ( 7839, 'KING', 'PRESIDENT', NULL, '1981-11-17'
, 5000, NULL, 10);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,DEPTNO ) VALUES ( 7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08'
, 1500, 0, 30);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,DEPTNO ) VALUES ( 7876, 'ADAMS', 'CLERK', 7788, '1987-05-23'
, 1100, NULL, 20);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,DEPTNO ) VALUES ( 7900, 'JAMES', 'CLERK', 7698, '1981-12-03'
, 950, NULL, 30);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,DEPTNO ) VALUES ( 7902, 'FORD', 'ANALYST', 7566, '1981-12-03'
, 3000, NULL, 20);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,DEPTNO ) VALUES ( 7934, 'MILLER', 'CLERK', 7782, '1982-01-23'
, 1300, NULL, 10);
commit;
-- 3、插入薪水等级表数据
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);
commit;
SELECT
ename
FROM
emp e
JOIN ( SELECT deptno, max( sal ) sal FROM emp GROUP BY deptno ) a ON e.deptno = a.deptno
AND e.sal = a.sal
SELECT
e.ename,
e.sal,
a.*
FROM
emp e
JOIN ( SELECT deptno, avg( sal ) avgSal FROM emp GROUP BY deptno ) a ON e.deptno = a.deptno
AND e.sal > a.avgSal
-- 平均的薪水等级:先计算每一个薪水的等级,然后找出薪水等级的平均值。
-- 平均薪水的等级:先计算平均薪水,然后找出每个平均薪水的等级值。
-- 第一步:获取每个人的薪水等级
-- 第二步:基于以上的结果继续按照deptno分组,求grade的平均值
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
-- 第一种方案:sal降序,limit
select sal from emp order by sal desc limit 1
-- 第二种方案:
select max(sal) from emp;
-- 第三种方案:
select sal from emp where sal not in(
select distinct a.sal from emp a
join emp b on a.sal <b.sal )
第一种方案:降序取第一个
SELECT
a.deptno
FROM
( SELECT deptno, avg( sal ) sal FROM emp GROUP BY deptno ) a
ORDER BY
sal DESC
LIMIT 1
第二种方案:max
SELECT
a.deptno,
max( a.sal ) maxSal
FROM
( SELECT deptno, avg( sal ) sal FROM emp GROUP BY deptno ) a
SELECT
d.dname,
avg( e.sal ) avgSal
FROM
emp e
LEFT JOIN dept d ON e.deptno = d.deptno
GROUP BY
e.deptno
ORDER BY
avgSal DESC
LIMIT 1
select a.*,s.grade from
(select d.dname,avg(sal) avgSal from emp e
left join dept d on e.deptno = d.deptno
group by d.dname) a join salgrade s on a.avgSal between s.losal and s.hisal
where grade =
(select grade from salgrade
where (select avg(sal) avgSal from emp group by deptno order by avgSal asc limit 1) between losal and hisal)
-- 比普通员工的最高薪水还要高的一定是领导!√
-- not in在使用的时候,后面小括号中记的排除null
SELECT ename, sal FROM emp WHERE sal > (
SELECT
max( sal )
FROM
emp
WHERE
empno NOT IN ( SELECT DISTINCT mgr FROM emp WHERE mgr IS NOT NULL )
)
select ename,sal from emp
order by sal desc
limit 5
select ename,sal from emp
order by sal desc
limit 5,5
日期也可以降序,升序
select hiredate from emp
order by hiredate desc
limit 5
select a.grade,count(a.ename) num from
(select e.ename,e.sal,s.grade from emp e
left join salgrade s on e.sal between s.losal and s.hisal)a
group by a.grade
select e.ename,m.ename leader from emp e
left join emp m on e.mgr = m.empno
select e.ename '员工',e.HIREDATE,m.ename '领导',m.hiredate,d.dname from emp e
left join emp m on e.mgr = m.empno
left join dept d on e.deptno = d.deptno
where e.hiredate < m.HIREDATE
select d.dname,e.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm from dept d
left join emp e on d.deptno = e.deptno
select deptno,count(empno) num from emp
group by deptno
having num>=5
select deptno num from emp
group by deptno
having count(*)>=5
select e.ename,e.job,e.mgr,e.hiredate,sal,deptno from emp e
where sal> (select sal from emp where ename = 'SMITH')
select a.ename,a.dname,b.num from
(select e.ename,e.deptno,d.dname from emp e
left join dept d on e.deptno = d.deptno
where e.job = 'CLERK') a left join
(select deptno,count(ename) num from emp
group by deptno) b on a.deptno = b.deptno
select job,count(*) from emp group by job having min(sal) > 1500;
select ename from emp e
left join dept d on e.deptno = d.deptno
where d.dname ='SALES'
select ename from emp
where deptno =(select deptno from dept where dname ='SALES')
select e.ename '员工',d.dname, m.ename '领导',s.grade from emp e
left join dept d on e.deptno = d.deptno
left join emp m on e.mgr = m.empno
left join salgrade s on e.sal BETWEEN s.LOSAL and s.hisal
where e.sal> (select avg(sal) avgSal from emp )
select e.ename,d.dname
from emp e
left join dept d on e.deptno = d.deptno
where e.job in (select job from emp
where ename = 'SCOTT') and e.ename <> 'SCOTT'
select ename,sal,deptno from emp
where deptno != 30
and sal in
(select distinct sal from emp
where deptno =30)
select e.ename,e.sal,d.dname from emp e
left join dept d on e.deptno = d.deptno
where e.sal >
(select max(sal) from emp where deptno =30)
-- 在mysql当中怎么计算两个日期的"年差",差了多少年:TimeStampDiff(间隔类型,前一个日期,后一个日期)
-- TimeStampDiff(YEAR,hiredate,now())
-- 间隔类型:SECOND 秒,MINUTE 分钟,HOUR 小时,DAY 天,WEEK 星期,MONTH 月,QUARTER 季度,YEAR 年
select d.*,count(e.ename) number,ifnull(avg(sal),0) avgSal,ifnull(avg(TimeStampDiff(YEAR,hiredate,now())),0) avgServiceTime from dept d
left join emp e on d.deptno = e.deptno
group by d.deptno,d.dname,d.loc
select e.ename,d.dname,e.sal from emp e
left join dept d on e.deptno = d.deptno
select d.deptno,d.dname,count(ename) num from dept d
left join emp e on d.deptno = e.deptno
GROUP BY d.deptno
select e.ename,a.* from emp e
join (select job,min(sal) lowSal
from emp
group by job) a on e.job = a.job and e.sal = a.lowSal
select deptno,min(sal) as minSal from emp
where job ='MANAGER'
GROUP BY deptno
SELECT
ename,(
sal + ifnull( comm, 0 ))* 12 AS yearSal
FROM
emp
ORDER BY
yearSal
SELECT
e.ename,
m.ename leader_name
FROM
emp e
JOIN emp m ON e.mgr = m.empno
WHERE
m.sal > 3000
SELECT
d.deptno,
d.dname,
ifnull( sum( e.sal ), 0 ) sumSal,
count( e.ename ) num
FROM
emp e
RIGHT JOIN dept d ON e.deptno = d.deptno
WHERE
d.dname LIKE '%S%'
GROUP BY
d.deptno,
d.dname
update emp set sal = sal * 1.1 where timeStampDiff(YEAR,hiredate,now()) >30
https://www.bilibili.com/video/BV1Vy4y1z7EX