【SQL】多表查询案例

📢本章节主要学习使用SQL多表查询的案例,多表查询基础概念 请点击此处。

 🎄数据准备

  • 首先我们创建一个新的表也就是薪资等级表,其余两个表(员工表和薪资表)在多表查询章节中已经创建。
  • 然后我么根据这三个表完成下面的12个需求。
create table salaryGrade(
    grade int,
    losal int,
    hisal int
)comment '薪资等级表';

insert into salaryGrade values (1,0,3000),
 (2,3001,5000),
 (3,5001,8000),
 (4,8001,10000),
 (5,10001,15000),
 (6,15001,20000),
 (7,20001,25000),
 (8,25001,30000);

🎄案例

⭐案例1

. 📢 查询员工的姓名、年龄、职位、部门信息 (隐式内连接)
select employee.name,employee.age,employee.job,department.name from employee,department
where employee.dept_id = department.id;

⭐案例2

📢 查询年龄小于30 岁的员工的姓名、年龄、职位、部门信息(显式内连接)
select e.name,e.age,e.job,d.name from
employee as e join department as d
    on (e.dept_id = d.id and e.age < 30);
  • 还有一种是在on子句后面加where条件
select e.name,e.age,e.job,d.name from
employee as e join department as d
    on e.dept_id = d.id where e.age > 30;

⭐案例3

📢 查询拥有员工的部门ID 、部门名称
  • 这里有个重点要使用distinct对查出的列进行去重操作。
  • 对于distinct来说是它后面所有的列都完全相同时才会去重。
select distinct department.id, department.name from employee,department
where employee.dept_id = department.id

⭐案例4

📢查询所有年龄大于40岁的员工, 及其归属的部门名称; 如果员工没有分配部门, 也需要展示出

( 外连接 )
select e.*,department.name from (select * from employee where employee.age > 40) as e
    left outer join department on e.dept_id = department.id;
  • 还有一种实现方式更为简单
select employee.*,department.name from employee left outer join department
    on employee.dept_id = department.id where employee.age > 40

⭐案例5

📢查询所有员工的工资等级

select employee.*,salaryGrade.grade from employee,salarygrade
                              where employee.salary between salaryGrade.losal and salaryGrade.hisal;

⭐案例6

📢 查询 " 研发部 " 所有员工的信息及 工资等级
  • 首先涉及到3个表,3个表的连接条件至少有两个,先确定连接条件
  • 连接条件:(employee.salary between salaryGrade.losal and salaryGrade.hisal) and department.id = employee.dept_id
  • 查询条件:department.name = '研发部'
select employee.*,salaryGrade.grade from employee ,department,salaryGrade
where  (employee.salary between salaryGrade.losal and salaryGrade.hisal) and department.id = employee.dept_id
and department.name = '研发部'

⭐案例7

📢 "研发部 " 员工的平均工资
select department.name, avg(employee.salary) as '平均工资'
from employee,
     department
where employee.dept_id = department.id
  and department.name = '研发部';

/* 每个部门平均工资 */
select department.name, avg(employee.salary) as '平均工资'
from employee,
     department
where employee.dept_id = department.id
group by employee.dept_id;

⭐案例8

📢查询工资比灭绝高的员工
  • 这是一个典型的标量子查询,因为返回的值只有一个值。
select name from employee where salary > (
select salary from employee where name = '灭绝')
  • 当然这个也可以使用自查询,只不过比子查询要复杂
select a.name from employee as a join employee as b on a.salary > b.salary where  b.name = '灭绝'

⭐案例9

📢 查询比平均薪资高的员工信息
select name from employee where salary > (select avg(salary) from employee);

⭐案例10

📢 查询低于本部门平均工资的员工信息
select * from employee as a where a.salary <
(select avg(b.salary) from employee  as b where b.dept_id = a.dept_id)
  • 还可以使用分组查询+自连接
select a.* from employee as a join
(select employee.dept_id,avg(employee.salary) as avg_salary from employee group by dept_id )
as b on a.dept_id = b.dept_id
where a.salary < b.avg_salary

⭐案例11

📢 查询所有的部门信息 , 并统计部门的员工人数
  • 这里使用的子查询属于select类型。
select a.id, a.name, (select count(*)  from employee as b where b.dept_id = a.id) as '部门人数'
from department as a;

⭐案例12

📢 查询所有学生的选课情况 , 展示出学生名称 , 学号 , 课程名称
  • 首先我们先把三张表的连接条件写出来。
  • 然后再去写表的查询条件。这个案例不需要写查询条件。
select student.name, student_course.studentno, student_course.courseno
from student,
     course,
     student_course
where student.no = student_course.studentno
  and course.name = student_course.courseno

    本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/973253.html

    如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

    相关文章

    Nginx(详解以及如何使用)

    目录 1. 什么是Nginx&#xff1f; 2. 为什么使用nginx? 3. 安装nginx 3.1?安装nginx的依赖插件 3.2 下载nginx ?3.3?创建一个目录作为nginx的安装路径 ?3.4?解压 ?3.5?进入解压后的目录 3.6?指定nginx的安装路径 ?3.7?编译和安装nginx 3.8 启动nginx ?…

    STM32 HAL库标准库+ESP8266+机智云

    前言 最近在项目中需要云平台对接&#xff0c;前面使用的是阿里云物理平台&#xff0c;具体开发可以看看我的这个文章&#xff1a;手把手教会使用阿里云平台&#xff0c;不过好像没有可以在手机很方便打开连接的&#xff0c;所以我在网上找到一些资料&#xff0c;发现机智云是…

    【前端框架】Vue3 面试题深度解析

    本文详细讲解了VUE3相关的面试题&#xff0c;从基础到进阶到高级&#xff0c;分别都有涉及&#xff0c;希望对你有所帮助&#xff01; 基础题目 1. 简述 Vue3 与 Vue2 相比有哪些主要变化&#xff1f; 答案&#xff1a; 响应式系统&#xff1a;Vue2 使用 Object.definePrope…

    DarkLabel 2.4使用指南:高效标注视频数据目标检测标签

    工具概述 DarkLabel 2.4 是一款强大的多功能标注工具&#xff0c;专为计算机视觉开发者设计&#xff0c;旨在提升标注工作的效率和精确度。其智能标注引擎支持两项核心功能&#xff1a;线性插值标注与多目标跟踪&#xff0c;极大地优化了视频标注过程。 &#xff08;1&#x…

    js解析后端传来的如图示的list集合,怎么获取每个map的key和value

    如图示&#xff0c;后端传到前端的questTypeList是一个HashMap的list集合 使用c标签将传来的集合放到下拉单选框中&#xff0c; <% taglib prefix"c" uri"http://java.sun.com/jsp/jstl/core" %><html> <body><form action"yo…

    SpringSecurity请求流转的本质

    1. SpringSecurity核心源码分析 分析SpringSecurity的核心原理,那么我们从哪开始分析?以及我们要分析哪些内容? 系统启动的时候SpringSecurity做了哪些事情?第一次请求执行的流程是什么?SpringSecurity中的认证流程是怎么样的?1.1 系统启动 当我们的Web服务启动的时候,…

    [论文阅读] SeeSR: Towards Semantics-Aware Real-World Image Super-Resolution

    文章目录 一、前言二、主要贡献三、Introduction四、Methodology4.1 Motivation &#xff1a;4.2Framework Overview.** 一、前言 通信作者是香港理工大学 & OPPO研究所的张磊教授&#xff0c;也是图像超分ISR的一个大牛了。 论文如下 SeeSR: Towards Semantics-Aware Rea…

    在VS中通过vcpkg包管理器来安装使用qt5

    常用指令 .\vcpkg install 库名 .\vcpkg install 库名版本号.\vcpkg install 库名 --trip x86-windows.\vcpkg list.\vcpkg search 库名 .\vcpkg x-all-installed --7zip PS G:\vcpkg> .\vcpkg help usage: vcpkg <command> [--switches] [--optionsvalues] [argume…

    ESXI 8.0 linux vSphere Client service has stopped working.手动启动服务

    1、首先在你的esxi中进入到你的VC系统中&#xff0c;我这个是linux部署 #查看每个服务及状态 service-control --status2、你会发现有停止的服务和正在启动的&#xff0c;在不知道具体哪些服务具体负责的功能&#xff0c;那你就一个一个起&#xff0c;边起边试----重要服务我后…

    基于SpringBoot+Vue的装修装潢管理系统的设计与实现

    获取源码&#xff1a;SpringBootVue的装修装潢公司管理系统: 用户&#xff1a;登录、注册、忘记密码、首页、产品展示、装修案例、装修套餐、装修预约、新闻动态、合作伙伴、在线留言、我的装修、个人中心、我的留言、我的预约、关于我们等功能管理员&#xff1a;登录、首页、户…

    DeepSeek部署到本地(解决ollama模型下载失败问题)

    一、下载ollama软件安装 1、下载ollama软件 Ollama 下载完成后可以直接进行安装&#xff08;外网&#xff0c;速度可能会有点慢&#xff09; 2、修改安装目录 进去下载的目录&#xff0c;使用cmd打开终端输入OllamaSetup.exe /DIRE:\MySoftware\Ollama 输入完成后会自动打开…

    【拥抱AI】GPT Researcher的诞生

    一、GPT Researcher 研究过程总结 GPT Researcher 是一个开源的自主智能体&#xff0c;旨在通过利用人工智能技术实现高效、全面且客观的在线研究。它通过一系列创新的设计和优化&#xff0c;解决了传统研究工具&#xff08;如 AutoGPT&#xff09;中存在的问题&#xff0c;如…

    什么是Dubbo?Dubbo框架知识点,面试题总结

    本篇包含什么是Dubbo&#xff0c;Dubbo的实现原理&#xff0c;节点角色说明&#xff0c;调用关系说明&#xff0c;在实际开发的场景中应该如何选择RPC框架&#xff0c;Dubbo的核心架构&#xff0c;Dubbo的整体架构设计及分层。 主页还有其他的面试资料&#xff0c;有需要的可以…

    软件单元测试的技术要求

    文章目录 一、软件单元测试的概念二、测试对象三、测试目的四、进入条件五、测试内容六、测试环境七、测试实施方一、软件单元测试的概念 单元测试(Unit Testing),是指对软件中的最小可测试单元进行测试验证。单元测试是白盒测试,主要依据软件详细设计和软件代码进行,不仅…

    GPT-Sovits:语音克隆训练-遇坑解决

    前言 本来以为3050完全无法执行GPT-Sovits训练的&#xff0c;但经过实践发现其实是可以&#xff0c;并且仅花费了十数分钟便成功训练和推理验证了自己的语音模型。 官方笔记&#xff1a;GPT-SoVITS指南 语雀 项目地址&#xff1a;https://github.com/RVC-Boss/GPT-SoVITS 本人…

    如何调用 DeepSeek API:详细教程与示例

    目录 一、准备工作 二、DeepSeek API 调用步骤 1. 选择 API 端点 2. 构建 API 请求 3. 发送请求并处理响应 三、Python 示例&#xff1a;调用 DeepSeek API 1. 安装依赖 2. 编写代码 3. 运行代码 四、常见问题及解决方法 1. API 调用返回 401 错误 2. API 调用返回…

    成员函数定义后面加const是什么功能:C++中const成员函数的作用

    成员函数定义后面加const是什么功能&#xff1a;C中const成员函数的作用 前言C中const成员函数的作用总结 前言 在PX4的代码中的位置控制模块中&#xff0c;有这样一个成员函数 void getAttitudeSetpoint(vehicle_attitude_setpoint_s &attitude_setpoint) const;该函数的…

    数据结构-----双向链表

    一、双向循环列表 head.h #ifndef __head_h__ #define __head_h__ #include <stdio.h> #include <string.h>…

    基于Flask的第七次人口普查数据分析系统的设计与实现

    【Flask】基于Flask的第七次人口普查数据分析系统的设计与实现&#xff08;完整系统源码开发笔记详细部署教程&#xff09;✅ 目录 一、项目简介二、项目界面展示三、项目视频展示 一、项目简介 基于Flask的人口普查可视化分析系统 二、项目界面展示 登录/注册 首页/详情 …

    纯手工搭建整套CI/CD流水线指南

    目录 一、前言 二、环境准备 1、服务器开荒&#xff08;192.168.1.200&#xff09; 2、离线资源清单&#xff08;提前用U盘拷好&#xff09; 三、硬核安装&#xff1a;比拧螺丝还细的步骤 Step1&#xff1a;搭建GitLab&#xff08;注意&#xff01;这是只内存饕餮&#xf…