首先我们使用MySQL创建两个表

– 创建人力资源管理系统数据库

drop database if exists hrs;
create database hrs default charset utf8;

– 切换数据库上下文环境

use hrs;

– 删除表

drop table if exists tb_emp;
drop table if exists tb_dept;

– 创建部门表

create table tb_dept
(
dno int comment '部门编号',
dname varchar(10) not null comment '部门名称',
dloc varchar(20) not null   comment '部门所在地',
PRIMARY key (dno)
);

– 添加部门记录

insert into tb_dept values (10, '会计部', '北京'),(20, '研发部', '成都'),(30, '销售部', '重庆'),(40, '运维部', '深圳');

– 创建员工表

create table tb_emp
(
eno int comment '员工编号',
ename varchar(20) not null comment '员工姓名',
job varchar(20) not null comment '员工职位' ,
mgr int comment '主管编号',
sal int not null comment '员工工资',
comm int comment '每月补贴',
dno int comment '所在部门编号',
primary key (eno),
foreign key (dno) REFERENCES tb_dept(dno)
);

– 添加员工记录

insert into tb_emp values
(7800, '张三丰', '总裁', null, 9000, 1200, 20),
(2056, '乔峰', '分析师', 7800, 5000, 1500, 20),
(3088, '李莫愁', '设计师', 2056, 3500, 800, 20),
(3211, '张无忌', '程序员', 2056, 3200, null, 20),
(3233, '丘处机', '程序员', 2056, 3400, null, 20),
(3251, '张翠山', '程序员', 2056, 4000, null, 20),
(5566, '宋远桥', '会计师', 7800, 4000, 1000, 10),
(5234, '郭靖', '出纳', 5566, 2000, null, 10),
(3344, '黄蓉', '销售主管', 7800, 3000, 800, 30),
(1359, '胡一刀', '销售员', 3344, 1800, 200, 30),
(4466, '苗人凤', '销售员', 3344, 2500, null, 30),
(3244, '欧阳锋', '程序员', 3088, 3200, null, 20),
(3577, '杨过', '会计', 5566, 2200, null, 10),
(3588, '朱九真', '会计', 5566, 2500, null, 10);

下面是查询语句:

– 查询薪资最高的员工姓名和工资

这里是引用

-- select ename, sal from tb_emp order by sal desc limit 1;
select ename, sal from tb_emp
where sal=(select max(sal) from tb_emp);

– 查询员工的姓名和年薪((工资+补贴)*12)

select ename, (sal+ifnull(comm, 0))*12 as annsal
from tb_emp order by annsal desc;

– 查询有员工的部门的编号和人数

select dno, count(dno) as total from tb_emp group by dno;

– 查询所有部门的名称和人数
– 当列有二义性的时候需要使用完全限定名

select dname, total from tb_dept t1,
(select dno, count(dno) as total from tb_emp group by dno) t2
where t1.dno=t2.dno;select dname as 部门名称, ifnull(total, 0) as 人数
from tb_dept t1 left outer join
(select dno, count(dno) as total from tb_emp group by dno) t2
on t1.dno=t2.dno;

– 查询薪资最高的员工(Boss除外)的姓名和工资

select ename, sal from tb_emp
where sal=(select max(sal) from tb_emp where mgr is not null);

– 查询薪水超过平均薪水的员工的姓名和工资

select ename, sal from tb_emp
where sal>(select avg(sal) from tb_emp);

– 查询薪水超过其所在部门平均薪水的员工的姓名、部门编号和工资

select ename, t1.dno, sal, sal-avgsal as delta
from tb_emp t1 inner join
(select dno, avg(sal) as avgsal
from tb_emp group by dno) t2 on t1.dno=t2.dno
where sal>avgsal;select ename, dno, sal, sal-avgsal as delta
from tb_emp t1 inner join
(select dno as deptno, avg(sal) as avgsal
from tb_emp group by dno) t2 on deptno=dno
where sal>avgsal;

– 查询部门中薪水最高的人姓名、工资和所在部门名称

select ename, sal, dname from tb_dept t3 inner join
(select ename, sal, t1.dno from tb_emp t1 inner join
(select dno, max(sal) as maxsal
from tb_emp group by dno) t2 on t1.dno=t2.dno
where sal=maxsal) t4 on t3.dno=t4.dno;

– 查询主管的姓名和职位

select ename, job from tb_emp where eno in
(select distinct mgr from tb_emp where mgr is not null);

– 集合运算和去重操作性能都非常糟糕应该尽量避免使用
– 可以使用exists和not exists来替代集合运算和去重

select ename, job from tb_emp t1 where exists
(select 'x' from tb_emp t2 where t1.eno=t2.mgr);select ename, job from tb_emp t1 where not exists
(select 'x' from tb_emp t2 where t1.eno=t2.mgr);

– 查询薪资排名4~6名的员工姓名和工资

select ename, sal from tb_emp order by sal desc limit 3,3;
select ename, sal from tb_emp order by sal desc limit 3 offset 3;

SQL中的查询语句总结(实例)相关推荐

  1. SQL:简单查询语句操作实例

    一.SQL Server简单查询语句 背景知识: 一.查询:SQL中最基本.最常用的操作,用来对数据库进行查询 二.表达式: select 属性列 from 表 where 筛选条件 group by ...

  2. sql中在查询语句中加判断,控制输出的内容

    Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END ...

  3. sql中模糊查询的字段中包含百分号%的语句

    sql中模糊查询的字段中包含百分号%的语句: select * from 表名 where 字段 like '%\%%' ESCAPE '\'; Mysql里用\%,比如: select * from ...

  4. SQL语言中的查询语句整理

    1. 查询语句的介绍: 本篇主要介绍sql语句里的查询语句,这其中包括了查询语句中的子内容,也就是拓展的内容,这些内容代入到查询语句中将会有更多的反应. 2. 查询语句: FROM命令: SELECT ...

  5. php sql查询两个表语句,sql多表查询语句与方法

    sql多表查询有很多种方法,如有自然连接 INNER JOIN,外边查询LEFT JOIN,交叉查询JOIN,交叉连接JOIN等join on left on 等多的是哦. sql多表查询语句与方法 ...

  6. 详解SQL中Groupings Sets 语句的功能和底层实现逻辑

    前言 SQL 中  Group By  语句大家都很熟悉, 根据指定的规则对数据进行分组 ,常常和 聚合函数 一起使用. 比如,考虑有表  dealer ,表中数据如下: 如果执行 SQL 语句  S ...

  7. SQL经典50查询语句案例_4(4、查询姓“李”的老师的个数)

    SQL经典50查询语句案例_4: 4.查询姓"李"的老师的个数: SELECT COUNT(Tname) FROM teacher WHERE Tname LIKE '李%' 在M ...

  8. sql 中的with 语句使用

    一直以来都很少使用sql中的with语句,但是看到了一篇文章中关于with的使用,它的确蛮好用,希望以后记得使用这个语句. 一.with 的用法 With alias_name as (select1 ...

  9. python的for语句用法_python中list循环语句用法实例

    本文实例讲述了python中list循环语句用法.分享给大家供大家参考.具体用法分析如下: Python 的强大特性之一就是其对 list 的解析,它提供一种紧凑的方法,可以通过对 list 中的每个 ...

最新文章

  1. Python 实现整数线性规划:分枝定界法(Branch and Bound)
  2. iOS UI-IOS开发中Xcode的一些使用技巧
  3. 常量指针与指向常量的指针
  4. 河南计算机程序大赛,我院成功举办河南省第十一届ACM大学生程序设计竞赛
  5. java HelloWorld 编程风格实践
  6. 做人不能太忽悠 评淘宝团购 续集
  7. python中keys是什么意思_python中key指的是什么
  8. jooq_jOOQ API设计缺陷的怪异事件
  9. 三同轴连接器_一种毫米波频段微带同轴转换结构
  10. 一段简单的代码告诉你什么叫内存溢出
  11. sql简介_SQL表简介
  12. Java虚拟机内存管理
  13. 模糊C均值聚类算法(原理+Matlab代码)
  14. Andriod1.0无法被识别,更新为安卓 ADB 驱动
  15. Android 10.0去掉后台启动Service的限制
  16. 亲民地理38期-江西极顶武功山(上)
  17. css锚点定位不准确问题
  18. TensorFlow学习笔记12----Creating Estimators in tf.contrib.learn
  19. 客户满意度测评模型-「客户满意度指数模型」
  20. 机器学习教会我们的6个道理

热门文章

  1. 获取MPU9250九轴数据--以四轴飞行器姿态解算为例
  2. 机器学习(七) 自编码器
  3. 微信运动刷步数软件有哪些?微信运动刷步软件推荐[
  4. Sketch网页截屏插件设计开发
  5. IntelliJ IDEA 如何创建一个 Java 项目
  6. 计算机图文混合排版教学设计,《WORD图文混排》教学设计
  7. 我改回iPhone的13个理由
  8. linux基础知识总结(上)
  9. vt100 c语言控制,【转】C语言中控制printf的打印颜色实例及vt100的控制符文档
  10. html背景颜色图片,HTML背景颜色和背景图片