当前位置: 首页 > news >正文

多表查询-练习

image

image

-- 多表查询-练习1
-- 1.查询员工的姓名、年龄、职位、部门信息。(隐式内连接)
select e.name,e.age,e.job,d.* from emp e ,dept d where e.dept_id = d.id;
-- 2.查询年龄小于30岁的员工姓名、年龄、职位、部门信息。(显式内连接)
select e.name,e.age,e.job,d.* from emp e inner join dept d on e.dept_id = d.id where e.age < 30;
-- 3.查询拥有员工的部门ID、部门名称。
select distinct d.id,d.name from emp e ,dept d where e.dept_id = d.id;
-- 4.查询所有年龄大于40岁的员工,及其归属的部门名称;如果员工没有分配部门,也需要展示出来。
select e.name,d.name from emp e left join dept d on d.id = e.dept_id where e.age>40;
-- 5.查询所有员工的工资等级。
-- 表 emp,salgrade
-- 连接条件:emp.salary >= salgrade.losal and emp.salary <= salgrade.hisal
select e.*,s.grade from emp e,salgrade s where e.salary >= s.losal and e.salary <= s.hisal;
select e.*,s.grade from emp e,salgrade s where e.salary between s.losal and s.hisal;
-- 6.查询"研发部"所有员工的信息及工资等级。
-- 表 emp,salgrade,dept
-- 连接条件 emp.salary between salgrade.losal and salgrade.hisal,emp.dept_id = dept.id
select e.*,s.grade from emp e, dept d ,salgrade s  where e.dept_id = d.id and (e.salary between s.losal and s.hisal) and d.name = '研发部';
-- 7.查询"研发部"员工的平均工资.
select avg(salary) from emp e,dept d where e.dept_id = d.id and d.name = '研发部';
-- 8.查询工资比"灭绝"高的员工信息。
select salary from emp where name = '灭绝';
select * from emp where salary > (select salary from emp where name = '灭绝');
-- 9.查询比平均薪资高的员工信息。
select avg(salary) from emp;
select * from emp where salary > (select avg(salary) from emp);
-- 10.查询低于本部门平均工资的员工信息。
-- a.查询指定部门的平均薪资
select avg(e1.salary) from emp e1 where dept_id = 1;
select avg(e1.salary) from emp e1 where dept_id = 2;
-- b.查询低于本部门平均工资的员工信息
select * from emp e2 where e2.salary < (select avg(e1.salary) from emp e1 where e1.dept_id = e2.dept_id);
-- 11.查询所有的部门信息,并统计部门的员工人数。
-- a.查询指定部门的人数
select count(*) from emp where dept_id = 1;
-- b.查询所有的部门信息,并统计部门的员工人数。
select d.*,(select count(*) from emp e where e.dept_id = d.id) '人数' from dept d ;
-- 12.查询所有学生的选课情况,展示出学生名称,学号,课程名称
-- 表  student course student_course
-- 连接 student.id = student_course.studentid    course.id = student_course.courseid
select s.name,s.no,c.name from student s, student_course sc,course c where s.id = sc.studentid and c.id = sc.courseid;

image

image

10-12题好好理解

http://www.zskr.cn/news/30472.html

相关文章:

  • 小程序原创--基于微信开发者工具实现的猜谜游戏程序 - 教程
  • ReactUse 与ahook对比 - 实践
  • 遗传改良中的核心技术:交配设计
  • 分享二个实用正则
  • 国际水稻研究所推出 AI 驱动的全球杂交水稻育种与亲本筛选数字平台
  • AI巨头动态:从OpenAI收购到Meta裁员,我们看到了什么?
  • Nature Plants | 植物转录因子结合图谱,360个转录因子的近3000个全基因组结合位点图谱
  • 【MyBatis】MyBatis 报错:Parameter ‘xxx‘ not found - 实践
  • Python 潮流周刊#74:创下吉尼斯世界记录的 Python 编程课
  • 10.26保养
  • CCPC2024济南个人题解
  • 作品目录
  • 【笔记】在WPF中 BulletDecorator 的功能、采用方式并对比 HeaderedContentControl 与常见 Panel 布局的区别
  • 刷题日记—洛谷循环题单 1.数学思想在算法题中的应用: 2.回文数的判定:
  • U623471 暂未定题目(无数据)
  • MAC地址类型速记
  • 【题解】洛谷P14308 【MX-S8-T1】斐波那契螺旋
  • 实验二 现代C++编程初体验
  • MCP Gateway 综述与实战指南
  • 清晨的阳光刚染红天边,我就钻进了彩虹色的热气球吊篮
  • 深入解析:关于在博客页面添加live2d-widget的一些心得和踩过的坑
  • Android设备位置历史深度解析:本地存储与取证技术
  • LLM安全新威胁:为什么几百个毒样本就能破坏整个模型
  • 软件技术基础第二次作业
  • vue3 不同构建版本
  • 使用 Android NDK 获取 YUV420p摄像头原始数据
  • 高阳台一首
  • 文档扩展名.js .jsx .ts .tsx区别(JavaScript扩展名、React扩展名、TypeScript扩展名)
  • Elasticsearch 搭建(亲测) - 实践
  • React Native启动性能优化实战:Hermes + RAM Bundles + 懒加载 - 指南