首页 > 其他分享 >10.27 多表

10.27 多表

时间:2024-10-29 17:22:10浏览次数:5  
标签:多表 name 10.27 dept2 dept emp incoming select

已知2张基本表:部门表:dept (部门号,部门名称);员工表 emp(员工号,员工姓名,年龄,入职时间,收入,部门号)
1:dept表中有4条记录:
部门号(dept1) 部门名称(dept_name )
101 财务
102 销售
103 IT技术
104 行政
2:emp表中有6条记录:
员工号 员工姓名 年龄 入职时间 收入 部门号对应字段名称为: (sid name age worktime_start incoming dept2)
1789 张三 35 1980/1/1 4000 101
1674 李四 32 1983/4/1 3500 101
1776 王五 24 1990/7/1 2000 101
1568 赵六 57 1970/10/11 7500 102
1564 荣七 64 1963/10/11 8500 102
1879 牛八 55 1971/10/20 7300 103

1.列出每个部门的平均收入及部门名称;

结果:

emp: avg(incoming) ,dept_name

dept: dept_name group by

条件:group by dept_name

方法1:【有链接+条件(分组)】

select dept_name,avg(incoming) from dept right join emp on dept.dept1=emp.dept2 group by dept_name ;

2.财务部门的收入总和;

结果:

emp: sum(incoming)

dept :dept_name='财务'

方法1:select sum(incoming ) from dept left join emp on dept.dept1=emp.dept2 where dept_name="财务" ;

方法2: select sum(incoming) from emp where dept2=(select dept1 from dept where dept_name='财务' );

方法3:select sum(incoming) from (select * from dept right join emp on dept.dept1=emp.dept2 ) as a where dept_name='财务'
3.It技术部入职员工的员工号

结果:emp :sid

条件:dept dept_name='IT 技术部门’

方法1:select sid from dept inner join emp on dept.dept1=emp.dept2 where dept_name="IT技术";

方法2:select sid from emp where dept2=(select dept1 from dept where dept_name='IT技术' ) ;

4.财务部门收入超过2000元的员工姓名

结果:name

条件:dept : dept_name='财务' incoming>2000

方法1:select name from dept inner join emp on dept.dept1=emp.dept2 where dept_name="财务" and incoming>2000;

方法2:select name from emp where incoming>2000 and dept2=(select dept1 from dept where dept_name='财务')

方法3:select a.name from (SELECT * from dept INNER JOIN emp on dept.dept1=emp.dept2 where dept_name='财务' )as a where a.incoming>2000

5.找出销售部收入最低的员工的入职时间;

结果:worktime_start

条件:min(incoming) ,dept_name='销售'

方法1:SELECT woektime_start from dept left join emp on dept.dept1=emp.dept2 where dept_name="销售" and incoming=(SELECT min(incoming) from dept inner join emp on dept.dept1=emp.dept2 and dept2=(select dept1 from dept where dept_name='销售'))

方法2:

select worktime_start from emp where (dept2,incoming)in(select dept2,min(incoming) from emp where dept2=(select dept1 from dept where dept_name="销售"));

6.找出年龄小于平均年龄的员工的姓名,ID和部门名称(所有员工平均年龄)

结果: name sid dept_name

条件 :avg(age) , age

方法1:select name,sid,dept_name from dept right join emp on dept1=dept2 where age< (select avg(age) from emp);

方法2:select name,dept_name,age from emp LEFT JOIN dept on emp.dept2=dept.dept1 where age<(SELECT avg(age) from dept INNER JOIN emp on dept.dept1=emp.dept2 )

7.列出每个部门收入总和高于9000的部门名称

结果:dept_name

条件: sum(incoming)>9000 ,group by dept_name

方法1:select dept_name from emp INNER JOIN dept on emp.dept2=dept.dept1 group by dept_name having sum(incoming)>9000;

方法2:select a.dept_name from (select dept_name,sum(incoming) from dept LEFT JOIN emp on dept.dept1=emp.dept2 GROUP BY dept_name HAVING sum(incoming)>9000)a;

方法3:select dept_name from (select sum(incoming) sum1,dept_name from dept left join emp on dept.dept1=emp.dept2 group by dept_name)s where s.sum1>9000;

8.查出财务部门工资少于3800元的员工姓名

结果: name

条件:dept_name =' 财务' incoming<3800

方法1:select name,dept_name from dept inner join emp on dept.dept1=emp.dept2 where dept_name="财务" and incoming<3800;

方法2:select name from emp where dept2 =(select dept1 from dept where dept_name='财务') and incoming<3800;

9.求财务部门最低工资的员工姓名;

结果:name

条件:
dept: dept_name='财务'
emp: min( incoming)

方法1:

select name from dept right join emp on dept.dept1=emp.dept2 where dept_name="财务" and incoming=(select min(incoming) from dept right join emp on dept.dept1=emp.dept2 where dept_name="财务");

方法2 :(只能显示一个,有多个一样)

select name from emp,dept where dept1=dept2 and dept_name='财务' order by incoming limit 0,1;

方法3:

select name from emp where (incoming,dept2) = (select min(incoming),dept2 from dept join emp on dept1=dept2 group by dept_name having dept_name='财务');

10.找出销售部门中年纪最大的员工的姓名

结果: name

条件: dept_name='销售' max(age)

方法1:

select emp.name from emp left join dept on emp.dept2=dept.dept1 where age=(select max(age) from emp left join dept on emp.dept2=dept.dept1 where dept.dept_name="销售") and dept.dept_name="销售";

方法2:(缺陷:有多个结果,就显示一个)

select name from emp inner join dept on emp.dept2=dept.dept1 where dept_name='销售' ORDER BY age desc limit 1

方法3:

select b.name from (select * from dept join emp on dept1=dept2 where dept_name="销售") b where age=(select max(age) from dept join emp on dept1=dept2 where dept_name="销售");
11.求收入最低的员工姓名及所属部门名称:

结果: name , dept _name

条件: min(incoming)

方法1:

select name,dept_name from emp left join dept on dept1=emp.dept2
where incoming=(select min(incoming)from emp left join dept on dept.dept1=emp.dept2);

方法2:

SELECT name,dept_name from dept INNER JOIN emp on dept.dept1=emp.dept2 WHERE incoming=(SELECT min(incoming)from emp );

12.求李四的收入及部门名称

结果:incoming ,dept_name

条件:name='李四'

方法1:Select incoming,dept_name from dept inner join emp on dept1=dept2 where name='李四';

13.求员工收入小于4000元的员工部门编号及其部门名称
结果: dept2 ,dept_name

条件: incoming<4000

方法1:SELECT dept1,dept_name from dept INNER JOIN emp on dept.dept1=emp.dept2 WHERE incoming<4000;

方法2:

SELECT dept_name,dept1 from dept WHERE dept1 in (select dept2 from emp where incoming<4000) ;

14.列出每个部门中收入最高的员工姓名,部门名称,收入,并按照收入降序;

方法1:先求每个部门名称和最高工资,在使用合表区匹配(部门名称和薪资一样的员工)

select name,dept_name,incoming from emp join dept on dept1=dept2 where (dept2,incoming) in (select dept2,max(incoming) incoming from emp group by dept2) order by incoming desc;

方法2:

select name,incoming,dept_name from dept INNER JOIN
(SELECT emp.name,emp.incoming,emp.dept2 from emp RIGHT JOIN
(select MAX(incoming) as incoming ,dept2 from emp group by dept2)c
on emp.dept2=c.dept2 and emp.incoming=c.incoming)b
On dept.dept1=b.dept2 ORDER BY incoming DESC;

方法3:

select b.name,b.incoming,b.dept_name from (select MAX(incoming) as incoming ,dept2 from emp group by dept2)a INNER JOIN (select * from dept INNER JOIN emp where dept1=dept2 )b on a.incoming=b.incoming and a.dept2=b.dept2 ORDER BY b.incoming desc ;

方法4:(有缺陷的方法)先降序排出最高工资的人员,在分组,在派排序

select name,dept_name,incoming from (select *from dept a inner join emp b ON
a.dept1=b.dept2 ORDER BY incoming desc )s group by s.dept_name order by incoming
desc

15.求出财务部门收益最高的俩位员工的姓名,工号,收益

结果: name , sid ,incoming

条件:dept_name='财务' limit 0,2

方法1:

SELECT name,sid,incoming from dept INNER JOIN emp on dept.dept1=emp.dept2 WHERE dept_name="财务" ORDER BY incoming desc limit 0,2 ;

方法2:

select name,sid,incoming from (select * from emp join dept on dept1=dept2 where dept_name='财务' order by incoming desc )as a limit 0,2;

方法3:

SELECT NAME,sid,incoming from emp WHERE dept2=(SELECT dept1 FROM dept WHERE dept_name='财务') ORDER BY incoming desc LIMIT 2;

16.查询财务部低于平均收入的员工号与员工姓名:(全部员工平均收入)

结果: sid ,name

条件:dept_name='财务' avg(incoming)

方法1:

select sid,name from dept inner join emp on dept.dept1=emp.dept2 where incoming<(select avg(incoming)from dept right join emp on dept.dept1=emp.dept2) and dept_name="财务";

方法2:

select sid,name from (select * from dept LEFT JOIN emp on dept.dept1=emp.dept2 WHERE dept_name="财务")q where q.incoming<(select avg(incoming) from emp);

方法3:

SELECT sid,name,incoming from dept INNER JOIN emp on dept.dept1=emp.dept2 WHERE dept_name="财务" and incoming<(select avg(incoming) from emp);

方法4:

select name,sid from dept INNER JOIN emp on dept.dept1=emp1.dept2 where incoming<(select avg(incoming)from emp1 where dept2=(select dept1 from dept where dept_name="财务"));

17.列出部门员工数大于1个的部门名称;

结果: dept_name

条件: count(name)>1 ,group by dept_name

方法1:select dept_name from emp INNER JOIN dept on dept1=dept2 GROUP BY dept_name HAVING count(name)>1;

方法2:select dept_name from dept where dept1 in (select dept2 from emp group by dept2 having count(dept2)>1);

方法3:SELECT dept_name FROM (select dept_name,count(dept_name) c from dept INNER JOIN emp ON dept.dept1=emp.dept2 GROUP BY dept_name) a WHERE a.c>1;

方法4:select dept_name from dept inner join (SELECT count(dept2),dept2 from emp GROUP BY dept2 having count(dept2)>1)as a on dept.dept1=a.dept2

18.列出部门员工收入不超过7500,且大于3000的员工年纪及部门编号;

结果:age , dept2 或dept1

条件:incoming<=7500 ,incoming>3000

方法1:select age,dept2,incoming from emp where incoming<=7500 and incoming>3000; (单表)

方法2:select age,sid from dept left join emp on dept.dept1=emp.dept2 where incoming<=7000&&incoming>3000 ;

19.求入职于20世纪70年代的员工所属部门名称;

结果: dept_name

条件:woek_time like 197%

方法1:SELECT dept_name from dept INNER JOIN emp on dept.dept1=emp.dept2 WHERE woektime_start BETWEEN 1970 and 1979;

方法2:select dept_name from dept inner join emp on dept.dept1=emp.dept2 where woektime_start>=1970 and woektime_start<1980;

方法3:

select dept_name from dept left join emp on dept.dept1=emp.dept2 where woektime_start like "197%";

20.查找张三所在的部门名称;

结果:dept_name

条件:name='张三'

select dept_name from dept left join emp on dept.dept1=emp.dept2 where name="张三"

方法2:

select dept_name from dept where dept1=(select dept2 from emp where name='张三') ;

21.列出每一个部门中年纪最大的员工姓名,部门名称;

结果: name ,dept_name

条件: group by dept_name max(age)

方法1:SELECT dept_name,name from dept INNER JOIN emp on dept.dept1=emp.dept2 where (dept_name,age ) in (SELECT dept_name ,max(age) from dept INNER JOIN emp on dept.dept1=emp.dept2 GROUP BY dept_name);

方法2:select b.name,b.dept_name from (select MAX(age) as age ,dept2 from emp group by dept2)a INNER JOIN (select * from dept INNER JOIN emp where dept1=dept2 )b on a.age=b.age and a.dept2=b.dept2 ;

22.列出每一个部门的员工总收入及部门名称;

结果:dept_name , sum(incomig)

条件:group by dept_name ,

方法1:select sum(incoming),dept_name from dept left join emp on dept.dept1=emp.dept2 group by dept_name;

23.列出部门员工收入大于7000的员工号,部门名称;

结果:sid ,dept_name

条件:incoming>7000

方法1:SELECT sid,dept_name from dept INNER JOIN emp on dept.dept1=emp.dept2 WHERE incoming>7000;

24.找出哪个部门还没有员工入职;(左独有数据)

结果: dept_name

条件:name is null

方法1:SELECT dept_name from dept LEFT JOIN emp on dept.dept1=emp.dept2 WHERE name is null;

方法2:select dept_name from dept left join emp on dept.dept1=emp.dept2 group by dept_name having (count(sid)=0);

25.先按部门号大小排序,再依据入职时间由早到晚排序员工信息表 ;(二次排序)

结果:员工信息表 所有信息 *

emp :dept2 order by desc (降序)

emp : woek_time 时间是越小就越早(升序)

方法1:select * from dept left join emp on dept.dept1=emp.dept2 order by dept1 desc ,worktime_start asc;

方法2:select *from emp order by dept2 desc,woektime_start asc;

26.求出财务部门工资最高员工的姓名和员工号

结果:name ,sid

条件:max(incomg) ,dept_name='财务'

方法1:

select sid,name from dept left join emp on dept.dept1=emp.dept2 where dept_name="财务" and incoming=(select max(incoming) from dept left join emp on dept.dept1=emp.dept2 where dept_name="财务");

27.求出工资在7500到8500之间,年龄最大的员工的姓名和部门名称。

结果:name,dept_name

条件: incoming=< 7500, incoming>=8000 max(age)

方法1:SELECT name,dept_name FROM dept right JOIN emp on dept.dept1=emp.dept2 WHERE age=(SELECT max(age) from emp WHERE incoming BETWEEN 7500 and 8500) and incoming BETWEEN 7500 and 8500 ;、

方法2:

select name,dept_name from dept join emp on dept1=dept2 where incoming between 7500 and 8500 and (dept_name,age) in (select dept_name,max(age) from emp join dept on dept1=dept2 group by dept_name);

标签:多表,name,10.27,dept2,dept,emp,incoming,select
From: https://www.cnblogs.com/huhao20001007/p/18513981

相关文章

  • 帝国cms一句MySQL语句实现多表数据之和
    SQL语句:SELECTCOUNT(AA.id)AStotalFROM(SELECTidFROMwww_moban5_cn_ecms_newsUNIONALLSELECTidFROMwww_moban5_cn_ecms_xiazaiUNIONALLSELECTidFROMwww_moban5_cn_ecms_photoUNIONALLSELECTidFROMwww_moban5_cn_ecms_download)......
  • Excel-多表数据查找匹配(VLOOKUP)
    ......
  • 34. 过滤条件、多表查询、子查询
    1.过滤条件1.1过滤条件之having[1]概念HAVING子句用于对分组后的结果进行过滤。它通常与GROUPBY子句一起使用,在SELECT语句的聚合函数(如SUM(),AVG(),COUNT(),MAX(),MIN()等)之后应用条件。HAVING子句与WHERE子句类似,但HAVING适用于分组后的数据,而WHERE适用......
  • Excel-多表数据查找匹配(VLOOKUP)
    ......
  • 第10课 数据库之多表运用
    一、多表查询1、什么是多表关联查询从2个表或者更多的表中查询我们需要的数据2、多表连接的关系?(1)内连接(2)左连接(3)右连接(4)左独有数据(5)右独有数据(6)全外连接比如:a表:1,2,3b表:1,2,4内连接:显示左边12和右边12关联12左连接:显示左边1,2,3,右边12关联......
  • 2024.10.27~2024.11.3
    2024.10.27这么说吧,csp-s打的不好,是时候做出些调整了约法n章:1.在NOIP之前把ybt刷完,保守估计一天5道题2.一道题若超出一个半小时内没有A就换下一道题,并在博客中记录此题并整理思路,有时间补完3.模拟赛我的得分要有以下两种评估:切题得分和难题高分暴力得分4.禁用一个月B站,休息......
  • 10.21~10.27 总结
    联考:https://www.cnblogs.com/british-union/p/liankao.html做题:学了一下线段树3,现在可以默写对了。CSPS2过程14:26发了密码开始看题。先读了一遍,感觉T1T2是简单题,T3可能得出充分必要条件之后DS优化dp?T4很长,但是我最讨厌\(2^k\)相关题(包含位运算),并瞬间想到之前某次......
  • [2024.10.27鲜花/csp后记?] 夢をみせて
    [2024.10.27鲜花/csp后记?]夢をみせて\(csp\)这周非常涨\(rp\)的,牙龈肿了,可能是牙周炎,总之又去了趟医院,自高中后,12.2,刚刚被月轮踩出的数字,很有趣,也许什么时候的十二月二日,会是一个特别的日子呢,哪怕幸运还是不幸,继续刚刚的话题吧,自高中后,似乎隔三岔五就要跑一趟医院或诊所,单单是晚......
  • 深度学习周报(10.21-10.27)
    目录摘要Abstract1机器学习(复习)1.1训练过程1.2非线性模型1.3新模型优化1.4激活函数2深度学习基本概念2.1深度学习的本质2.2训练过程2.2.1定义一组函数(defineasetoffunction)2.2.2 评估函数的好坏(goodnessoffunction)2.2.2 选择最佳的函数(pi......
  • 约束、多表查询
    约束MySQL无检查约束案例在定义表的时候进行约束外键约束--创建表时添加外键约束CREATETABLE表名(列名数据类型,[CONSTRAINT][外键名称(fk_表1_表二)]FOREIGNKEY(外键列名)REFERENCES主表(主表列名)--建完表后添加外键约束ALTERTABLE表名ADDCONSTRAINT外键名......