参赛话题:学习笔记
个人主页:BoBooY的CSDN博客_Java领域博主 前言:在上一期中我们讲解了MySQL的入门知识点,但理论总还是要通过实践来印证,学了再多不练也是白费,这一期我们讲解MySQL的一些经典查询例题帮助大家巩固已学知识点,如在做题过程中有知识点的遗忘,可以参考往期文章: MySQL入门知识点(上):https://blog.csdn.net/qq_58233406/article/details/127143537 MySQL入门知识点(下):https://blog.csdn.net/qq_58233406/article/details/127144532 文章目录
select
d.deptno,count(e.ename) ,avg(e.sal),avg(timestampdiff(YEAR,e.hiredate,now()))
from
emp e
right join
dept d
on
e.deptno = d.deptno
group by
d.deptno;
select
e.ename,d.dname,e.sal
from
emp e
join
dept d
on
e.deptno = d.deptno;
select
*,count(ename)
from
emp e
right join
dept d
on
e.deptno = d.deptno
group by
d.deptno;
(1)查找最低工资的工作
select
job,min(sal)
from
emp
group by
job;
(2)将emp表与这个表进行外连接
select
e.ename,e.job,e.sal
from
emp e
join(
select
job,min(sal) minsal
from
emp
group by
job
) t
on
e.job = t.job and e.sal = t.minsal;
select
deptno,min(sal)
from
emp
where
job = 'MANAGER'
group by
deptno;
select
ename,(sal + ifnull(comm,0))* 12 yearsal
from
emp
order by
yearsal;
(1)求出员工领导的薪水超过3000的
select
a.ename '员工',a.sal '员工薪水',b.ename '领导',b.sal '领导工资'
from
emp a
join
emp b
on
a.mgr = b.empno
where
b.sal > 3000;
select
d.deptno,sum(ifnull(sal,0)) '工资合计',count(ename) '部门人数'
from
emp e
right join
dept d
on
e.deptno = d.deptno
where
d.dname like '%S%'
group by
e.deptno;
select
ename,timestampdiff(YEAR,hiredate,now()) '任职日期'
from
emp
where
timestampdiff(YEAR,hiredate,now()) > 30;
create table emp2 as select * from emp;
update
emp2
set
sal = sal*1.1
where
timestampdiff(YEAR,hiredate,now()) > 30;
练习题集:
MySQL经典练习题+解题思路(一):https://blog.csdn.net/qq_58233406/article/details/127150051
MySQL经典练习题+解题思路(二):https://blog.csdn.net/qq_58233406/article/details/127162943
MySQL经典练习题+解题思路(三):https://blog.csdn.net/qq_58233406/article/details/127165622