MySQL笔记及习题(五)--子查询

5.子查询
1.含义:出现在其他语句中的select语句,称为子查询或者内查询
2.分类:
	按子查询出现的位置:
			select后面:仅仅支持标量子查询
			from后面:支持表子查询
			where后者having后面:
					标量子查询(单行)
					列子查询(多行)
					行子查询
			exists后面(相关子查询):表子查询
	按结果集的行列数不同:
			标量子查询:结果集只有一行一列
			列子查询:结果集只有一列多行
			行子查询:结果集有一行多列
			表子查询:结果集一般为多行多列
3.wherehaving后面
	1.标量子查询(单行子查询)
	2.列子查询(多行子查询)
	3.行子查询(多行多列)
	特点:
		·子查询放在小括号内
		·子查询一般放在条件的右侧
		·标量子查询:一般搭配单行操作符使用(<,>,=,>=,<=,<>)
	列子查询:一般搭配着多行操作符使用(in,any/some,all)
	·子查询的执行顺序优先于主查询的执行,主查询的条件用到了子查询的结果
	
in,any/some,all
exists(有信息返回1,没有返回0)
  • 例题
--1.谁的工资比 Abel 高?
select * from employees where salary>( select salary from employees where last_name='Abel' );

--2.返回job_id与141号员工相同,salary比143号员工多的员工的姓名,job_id和工资
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
select last_name,job_id,salary from employees where salary=( SELECT min(salary) from employees );

--4.查询最低工资大于50号部门最低工资的部门id和其最低工资
select department_id,min(salary) from employees GROUP BY department_id HAVING min(salary)>( select min(salary) from employees where department_id=50 );

--5.返回其它工种中比job_id为‘IT_PROG’工种任一工资低的员工的员工号、姓名、job_id 以及salary
select employee_id,last_name,job_id,salary from employees where salary<any( 
	select DISTINCT salary from employees where job_id='IT_PROG' ) 
	and job_id!='IT_PROG';

--6.返回其它部门中比job_id为‘IT_PROG’部门所有工资都低的员工   的员工号、姓名、job_id 以及salary
select employee_id,last_name,job_id,salary from employees where salary<all( 
	select DISTINCT salary from employees where job_id='IT_PROG' ) 
	and job_id!='IT_PROG';
	
--7.查询员工编号最小并且工资最高的员工信息
select * from employees where employee_id=( 
	select min(employee_id) from employees) and salary=(
	select max(salary) from employees);

select min(employee_id),max(salary) from employees;
select * from employees where (employee_id,salary)=(select min(employee_id),max(salary) from employees);

--8.查询每个部门信息和员工个数
select d.*,(
 select count(*) from employees e where e.department_id=d.department_id
 ) 
 from departments d;

select d.*,( select count(*) from employees ) from departments d GROUP BY d.department_id;
 
 --9.查询每个部门的平均工资的工资等级
 #select salary,grade_level from employees e join job_grades on salary BETWEEN lowest_sal and highest_sal
select grade_level,ag.department_id from 
(
select avg(salary) pjgz,department_id from employees GROUP BY department_id
) ag,job_grades where ag.pjgz between lowest_sal and highest_sal;
***
select grade_level,ag.department_id from 
(
select avg(salary) pjgz,department_id from employees GROUP BY department_id
) ag inner join job_grades on ag.pjgz between lowest_sal and highest_sal;

--10.查询有员工的部门名
#in:
select department_name from departments where department_id in( select department_id from employees );
#exists:
select department_name from departments where exists (
select * from employees e where e.department_id=departments.department_id
);

--11.查询没有女朋友的男神信息
#in
SELECT bo.*
FROM boys bo
WHERE bo.id NOT IN(
	SELECT boyfriend_id
	FROM beauty
);
#exists
SELECT bo.*
FROM boys bo
WHERE NOT EXISTS(
	SELECT boyfriend_id
	FROM beauty b
	WHERE bo.`id`=b.`boyfriend_id`
);
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WGS.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值