请选择 进入手机版 | 继续访问电脑版
MSIPO技术圈 首页 IT技术 查看内容

MySQL每日一练:多表查询——连接查询、子查询

2023-07-13

目录

1、首先创建员工表emp和部门表dept:

dept表:

emp表: 

2、插入数据:

dept表:

emp表:

3、 按条件查找

1、首先创建员工表emp和部门表dept:

dept表:

create table dept (        
    dept1 int ,
    dept_name varchar(11)); 

emp表: 

create table emp (
    sid int ,
    name varchar(11),
    age int,
    worktime_start date,
    incoming int,
    dept2 int);

2、插入数据:

dept表:

 insert into dept values (101,'财务');
​ insert into dept values (102,'销售');
​ insert into dept values (103,'IT技术');
​ insert into dept values (104,'行政');

emp表:

insert into emp values(1789,'张三',35,'1980/1/1',4000,101);
insert into emp values(1674,'李四',32,'1983/4/1',3500,101);
insert into emp values(1776,'王五',24,'1990/7/1',2000,101);
insert into emp values(1568,'赵六',57,'1970/10/11',7500,102);
insert into emp values(1564,'荣七',64,'1963/10/11',8500,102);
insert into emp values(1879,'牛八',55,'1971/10/20',7300,103);

3、 按条件查找:

1.找出销售部门中年纪最大的员工的姓名
select name from emp where age=(select max(age)from emp);

 2.求财务部门最低工资的员工姓名
select name 
from emp 
where incoming=(select min(incoming) from emp 
inner join dept
on emp.dept2=dept.dept1 and dept_name='财务') ;

 3.列出每个部门收入总和高于8000的部门名称 
SELECT dept.dept_name as 部门名称,sum(emp.incoming) as 总收入 
FROM emp 
INNER JOIN dept 
ON dept.dept1 = emp.dept2 
GROUP BY dept.dept_name HAAVING 总收入 > 8000;
 4.求工资在7500到8500元之间,年龄最大的人的姓名及部门  
select emp.name,dept.dept_name,emp.age,emp.incoming
from emp 
inner join dept
on emp.dept2=dept.dept1 
where incoming between 7500 and 8500 and age=(select max(age) from emp);
5.找出销售部门收入最低的员工入职时间
select emp.worktime_start 
from emp 
inner join dept 
on emp.dept2=dept.dept1 where incoming=(select min(incoming) from emp);

6.财务部门收入超过2000元的员工姓名
select emp.name from emp 
inner join dept 
on emp.dept2=dept.dept1 where incoming>2000 and dept.dept_name='财务';

7.列出每个部门的平均收入及部门名称
select dept_name,avg(emp.incoming) as 部门平均收入 
from emp innerjoin dept 
on emp.dept2=dept.dept1  group by dept.dept_name;

8.IT技术部入职员工的员工号
select dept.dept_name,emp.sid 
from emp inner 
join dept 
on emp.dept2=dept.dept1  where dept.dept_name='IT技术';

9.财务部门的收入总和;
select dept.dept_name,sum(incoming) as ‘总收入’ 
from emp 
inner join dept 
on emp.dept2=dept.dept1  where dept.dept_name='财务';

10.先按部门号大小排序,再依据入职时间由早到晚排序员工信息表*  
select dept.dept_name,emp.dept2 
from emp 
inner join dept 
on emp.dept2=dept.dept1  order by dept.dept1;
select dept.dept_name,worktime_start 
from emp 
inner join dept 
on emp.dept2mp.dept2=dept.dept1  order by worktime_start;

11.找出哪个部门还没有员工入职; 
select * from emp where worktime_start is null;
 12.列出部门员工收入大于7000的部门编号,部门名称;
SELECT emp.dept2,dept.dept_name,incoming FROM emp INNER JOIN dept
ON emp.dept2 = dept.dept1 where incoming>7000;

13.列出每一个部门的员工总收入及部门名称; 
SELECT dept.dept_name as '部门名称',sum(incoming) as '部门员工总收入' 
FROM emp 
inner JOIN dept 
ON emp.dept2 = dept.dept1 group by dept.dept_name;

14.列出每一个部门中年纪最大的员工姓名,部门名称*
SELECT dept.dept_name,age 
FROM (SELECT dept2, MAX(age) AS max_age
    FROM emp 
    GROUP BY dept2 )
AS 
max_age_table 
INNER JOIN emp 
ON emp.dept2 = max_age_table.dept2 AND emp.age = max_age_table.max_age 
INNER JOIN dept 
ON emp.dept2 = dept.dept1;

15.求李四的收入及部门名称
SELECT emp.name,incoming,dept.dept_name as '部门名称'  
FROM emp 
INNER JOIN dept 
ON emp.dept2 = dept.dept1 where name='李四';

16.列出每个部门中收入最高的员工姓名,部门名称,收入,并按照收入降序*
SELECT emp.name,dept.dept_name,emp.incoming 
FROM (SELECT dept2,2, MAX(incoming) 
AS 
max_incoming 
FROM emp     
GROUP BY dept2 )
AS 
max__incoming_table 
INNER JOIN emp 
ON emp.dept2 = max_incoming_table.dept2 ANDD emp.incoming = max_incoming_table.max_incoming INNER JOIN dept 
ON emp.deept2 = dept.dept1 ORDER BY emp.incoming DESC;

17.列出部门员工数大于1个的部门名称*
SELECT dept.dept_name 
FROM dept 
INNER JOIN (SELECT dept2, COUNT(*) 
AS 
emp_count FROM emp GROUP BY dept2  HAVING COUNT(*) > 1 ) 
AS 
emp_count_table ON dept.dept1 = emp_count_table.dept2;

18.查找张三所在的部门名称
SELECT emp.name,dept.dept_name as '部门名称' 
FROM emp 
INNER JOINdept 
ON emp.dept2 = dept.dept1 where name='张三';

相关阅读

热门文章

    手机版|MSIPO技术圈 皖ICP备19022944号-2

    Copyright © 2024, msipo.com

    返回顶部