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

目录

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);

MySQL每日一练:多表查询——连接查询、子查询_第1张图片

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

MySQL每日一练:多表查询——连接查询、子查询_第2张图片

 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;
MySQL每日一练:多表查询——连接查询、子查询_第3张图片
 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);
MySQL每日一练:多表查询——连接查询、子查询_第4张图片 5.找出销售部门收入最低的员工入职时间
select emp.worktime_start 
from emp 
inner join dept 
on emp.dept2=dept.dept1 where incoming=(select min(incoming) from emp);

MySQL每日一练:多表查询——连接查询、子查询_第5张图片

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

MySQL每日一练:多表查询——连接查询、子查询_第6张图片

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

MySQL每日一练:多表查询——连接查询、子查询_第7张图片

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

MySQL每日一练:多表查询——连接查询、子查询_第8张图片

9.财务部门的收入总和;
select dept.dept_name,sum(incoming) as ‘总收入’ 
from emp 
inner join dept 
on emp.dept2=dept.dept1  where dept.dept_name='财务';
MySQL每日一练:多表查询——连接查询、子查询_第9张图片
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;

MySQL每日一练:多表查询——连接查询、子查询_第10张图片

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;

MySQL每日一练:多表查询——连接查询、子查询_第11张图片

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

MySQL每日一练:多表查询——连接查询、子查询_第12张图片

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;

MySQL每日一练:多表查询——连接查询、子查询_第13张图片

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

MySQL每日一练:多表查询——连接查询、子查询_第14张图片

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;

MySQL每日一练:多表查询——连接查询、子查询_第15张图片

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;

MySQL每日一练:多表查询——连接查询、子查询_第16张图片

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

MySQL每日一练:多表查询——连接查询、子查询_第17张图片

你可能感兴趣的:(MySQL,日常,mysql,数据库,学习)