数据库子查询 ---where或having后面----列子查询-多行子查询

Posted 陕西五花肉

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库子查询 ---where或having后面----列子查询-多行子查询相关的知识,希望对你有一定的参考价值。

#一.where或having后面
/*
1.标量子查询(当行子查询)
2.列子查询(多行子查询)

3.行子查询(多列多行)

特点:
 ①子查询放在小括号内
 ②子查询一般放在条件的右侧 
 ③标量子查询,一般搭配着当行操作符使用
	> < <= >= <>
 ④列子查询,一般搭配着多行操作符使用
 In and/some all
 
 
 */
 
 #1.标量子查询
 #案例1:谁的工资比Abel 高?
 #①查询Abel的工资
 SELECT salary
 FROM employees
 WHERE last_name='Abel'
 
 #②查询员工的信息  满足salary>①结果
 SELECT *
 FROM employees
 WHERE salary>(
	 SELECT salary
	 FROM employees
	 WHERE last_name='Abel'
 
 
 );
 
#案列2:返回job_id与141号员工相同,salary比143号员工多的员工 姓名,job_id和工资
#①查询141号员工的job_id
SELECT job_id 
FROM employees
WHERE employee_id=141;

#②查询143号员工的salary
SELECT salary
FROM employees
WHERE employee_id=143;

#③查询员工的姓名,job_id和工资,要求job_id=①并且salary>②

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 MIN(salary)
FROM employees

#②last_name,job_id和salary  要求salary=①
SELECT last_name,job_id,salary
FROM employees
WHERE salary=(
	SELECT MIN(salary)
	FROM employees
);

#案例4: 查询最低工资大于50号部门最低工资的部门id和其最低工资
#①查询50号部门的最低工资
SELECT MIN(salary)
FROM employees
WHERE department_id =50

#②查询每个部门的最低工资
SELECT MIN(salary),department_id
FROM employees
GROUP BY department_id
#③筛选②,满足min(salary)> ①
SELECT MIN(salary),department_id
FROM employees
GROUP BY department_id
HAVING MIN(salary)>(
	SELECT MIN(salary)
	FROM employees
	WHERE department_id =50

);

#2.列子查询(多行子查询)★
#案例1:返回location_id是1400或1700的部门中的所有员工姓名

#①查询location_id是1400或1700的部门编号
SELECT DISTINCT department_id
FROM departments
WHERE location_id IN(1400,1700)

#②查询员工姓名,要求部门号是①列表中的某一个

SELECT last_name
FROM employees
WHERE department_id  <>ALL(
	SELECT DISTINCT department_id
	FROM departments
	WHERE location_id IN(1400,1700)


);


#案例2:返回其它工种中比job_id为‘IT_PROG’工种任一工资低的员工的员工号、姓名、job_id 以及salary

#①查询job_id为‘IT_PROG’部门任一工资

SELECT DISTINCT salary
FROM employees
WHERE job_id = 'IT_PROG'

#②查询员工号、姓名、job_id 以及salary,salary<(①)的任意一个
SELECT last_name,employee_id,job_id,salary
FROM employees
WHERE salary<ANY(
	SELECT DISTINCT salary
	FROM employees
	WHERE job_id = 'IT_PROG'

) AND job_id<>'IT_PROG';

#或
SELECT last_name,employee_id,job_id,salary
FROM employees
WHERE salary<(
	SELECT MAX(salary)
	FROM employees
	WHERE job_id = 'IT_PROG'

) AND job_id<>'IT_PROG';


#案例3:返回其它部门中比job_id为‘IT_PROG’部门所有工资都低的员工   的员工号、姓名、job_id 以及salary

SELECT last_name,employee_id,job_id,salary
FROM employees
WHERE salary<ALL(
	SELECT DISTINCT salary
	FROM employees
	WHERE job_id = 'IT_PROG'

) AND job_id<>'IT_PROG';

#或

SELECT last_name,employee_id,job_id,salary
FROM employees
WHERE salary<(
	SELECT MIN( salary)
	FROM employees
	WHERE job_id = 'IT_PROG'

) AND job_id<>'IT_PROG';

以上是关于数据库子查询 ---where或having后面----列子查询-多行子查询的主要内容,如果未能解决你的问题,请参考以下文章

数据库子查询经典面试题

oracle数据库子查询的结果需要使用多次解决办法

在树型结构中如何从数据库中查询并显示子节点!

删除firebase数据库子节点时应用程序崩溃

数据库(比如MYSQL) ,表连结查询与子查询哪个效率高些? 为啥

查询数据库所有表,子段说明