【Mysql数据库基础05】子查询 where、from、exists子查询、分页查询

where、from、exists子查询、分页查询

  • [1 where子查询](#1 where子查询)
    • [1.1 where后面的标量子查询](#1.1 where后面的标量子查询)
      • [1.1.1 having后的标量子查询](#1.1.1 having后的标量子查询)
    • [1.2 where后面的列子查询](#1.2 where后面的列子查询)
    • [1.3 where后面的行子查询(了解即可)](#1.3 where后面的行子查询(了解即可))
  • [2 from子查询](#2 from子查询)
  • [3 exists子查询(相关子查询)](#3 exists子查询(相关子查询))
  • [4 分页查询](#4 分页查询)
  • [5 联合查询](#5 联合查询)
  • [6 练习](#6 练习)

1 where子查询

1.1 where后面的标量子查询

1.谁的工资比Abel高?

sql 复制代码
select *
from employees
where salary > (
	select salary
	from employees
	where last_name = 'Abel'
);

2.返回job_id与141号员工相同,salary比143号员工多的员工姓名,job_id 和工资

sql 复制代码
select last_name,job_id,salary
from employees
where job_id = (
	select job_id
	from employees
	where employee_id = 141
)
and salary > (
	select salary
	from employees
	where employee_id = 143
);

3.返回公司工资最少的员工的last_name,job_id和salary

sql 复制代码
select last_name,job_id,salary
from employees
where salary = (
	select min(salary)
	from employees
);

1.1.1 having后的标量子查询

查询最低工资大于50号部门最低工资的部门id和其最低工资

可以拆分去考虑

1.查询50号部门最低工资

sql 复制代码
select min(salary)
from employees
where department_id = 50;

2.查询每个部门的最低工资

sql 复制代码
select department_id,min(salary)
from employees
group by department_id;

3.合并

sql 复制代码
select department_id,min(salary)
from employees
group by department_id
having min(salary) > (
	select min(salary)
	from employees
	where department_id = 50
);

1.2 where后面的列子查询

单列多行

IN/NOT IN 任意一个

ANY/SOME 某一个

ALL 所有

IN 等于 = ANY

1.返回location_id是1400或1700的部门中的所有员工姓名

sql 复制代码
select last_name
from employees 
where department_id in (
	select distinct department_id
	from departments
	where location_id in (1400,1700)
);

2.返回其它工种中比job_id为'IT_PROG'工种任一工资低的员工的,工号、姓名、job_id 以及salary

sql 复制代码
select employee_id,last_name,job_id,salary
from employees
where salary < any (
	select salary
	from employees
	where job_id = 'IT_PROG'
) and job_id <> 'IT_PROG';

1.3 where后面的行子查询(了解即可)

一行多列或者多行多列

查询员工编号最小并且工资最高的员工信息

sql 复制代码
select *
from employees
where (employee_id,salary) = (
	select min(employee_id),max(salary)
	from employees
);

2 from子查询

查询每个部门的平均工资的工资等级

sql 复制代码
select avg_dep.department_id,avg_dep.avg,g.grade_level
from job_grades g
inner join (
	select department_id,avg(salary) as avg
	from employees
	group by department_id
)as avg_dep
on avg_dep.avg between g.lowest_sal and g.highest_sal;

3 exists子查询(相关子查询)

只关心有没有这个值

查询有员工的部门名

sql 复制代码
select department_name
from departments d
where exists(
		select *
		from employees e
		where d.department_id = e.department_id
);

4 分页查询

limit offset,size

offset 要显示条目的索引

1.查询前五条的员工信息

sql 复制代码
select *
from employees
limit 0,5; #起始索引是0,一共显示5条

2.查询第11条~第25条的员工信息

sql 复制代码
select *
from employees
limit 10,15;

3.有奖金的员工信息,并且工资较高的前10名显示出来

sql 复制代码
select *
from employees
where commission_pct is not null
order by salary desc
limit 10;

5 联合查询

查询中国用户中男性的信息以及外国用户中年男性的用户信息

sql 复制代码
select * from t_ca where csex='男'
union
select * from t_ua where tGender = 'male';

联合查询的特点:

1.要求多条查询语句的查询列数是一致的

2.要求多条查询语句查询的每一列的类型和顺序最好一致

3.union会去重

不想去重的话,可以使用union all关键字

6 练习

1.查询和zlotkey相同部门的员工姓名和工资

sql 复制代码
select last_name,salary
from employees
where department_id = (
	select department_id
	from employees
	where last_name = 'Zlotkey'
);

2查询工资比公司平均工资高的员工的员工号,姓名和工资。

sql 复制代码
select employee_id,last_name,salary
from employees
where salary > (
	select avg(salary)
	from employees
);

3查询各部门中工资比本部门平均工资高的员工的员工号,姓名和工资

sql 复制代码
select employee_id,last_name,salary
from employees e
inner join (
	select department_id,avg(salary) as ag
	from employees
	group by department_id
) avg_dep
on avg_dep.department_id = e.department_id
where salary > avg_dep.ag;

4.查询管理者是king的员工姓名和工资

sql 复制代码
select last_name,salary
from employees
where manager_id in(
	select employee_id
	from employees
	where last_name = 'K_ing'
);

5.查询工资最高的员工的姓名,要求first_name和iast_name显示为一列,列名为 姓.名

sql 复制代码
select concat(first_name,last_name) as '姓.名'
from employees
where salary = (
	select max(salary)
	from employees
);
相关推荐
FungLeo几秒前
Node 后端实战 · 列表查询到底怎么写?一个通用 DSL 封装,过滤分页排序一次搞定
数据库·node.js·serverless·dsl 封装·通用列表查询
抓蛙师28 分钟前
多租户 tenant_id SQL 注入漏洞分析与应急响应报告
数据库·sql
ACP广源盛1392462567332 分钟前
Qwen3.8‑2.4T 开源落地@ACP#国产 MoE 私有化部署下 GSV2221 视频转换芯片机遇分析
大数据·数据库·人工智能
七牛开发者2 小时前
为什么 Go 很适合 AI 辅助开发?
数据库·人工智能·python·elasticsearch·log4j
何以解忧,唯有..3 小时前
数据库索引失效的常见情况与优化策略
数据库·sql·oracle
这个DBA有点耶4 小时前
分布式数据库到底该不该上?从判断标准到架构选型的实战思考
数据库·架构·dba
01_ice5 小时前
MySQL库和表的操作
数据库·mysql
oradh5 小时前
Oracle UNDO表空间管理维护总结
数据库·oracle·undo表空间·undo表空间管理·undo表空间管理维护
布莱克6055 小时前
数据库索引分类:数据结构、物理存储与逻辑角度详解
数据结构·数据库
SelectDB5 小时前
DeepSeek Harness 接入 Litefuse:完善 Agent 可观测与评估能力
数据库