SQL 基础之使用子查询检索数据(二十二)

简介:

多列子查询

where (manager_id, department_id) in

子查询

100 90

102 60

124 50


主查询的每行都与多行和多列的子查询进行比较


列的比较

多列的比较,包含子查询可以是:

不成对比较

成对比较


成对比较子查询
1、显示与员工名为“John”同部门且同一个经理的其它员工信息

select employee_id, manager_id, department_id from empl_demo

where (manager_id, department_id) IN

(select manager_id, department_id from empl_demo

where first_name = 'John')

AND first_name <> 'John';


不成对比较

1、显示名字不为 “John”员工的经理ID和部门ID的员工号、经理号、部门号

select employee_id, manager_id, department_id

from empl_demo

where manager_id in

(select manager_id

from empl_demo

where first_name = 'john')

and department_id in

(select department_id

from empl_demo

where first_name = 'john')

and first_name <> 'john';


标量子查询表达式

标量子查询是从一行中返回一列的子查询

标量子查询可在下列情况下使用:

– DECODE 和 CASE 条件和表达式的一部分

– SELECT 中除 GROUP BY 子句以外的所有子句中

– UPDATE 语句的 SET 子句和 WHERE 子句


CASE 表达式中的标量子查询:

select employee_id, last_name, department_id,

(case

when department_id =

(select department_id

from departments

where location_id = 1800)

then 'canada' else 'usa' end) location

from employees;


ORDER BY 子句中的标量子查询:

select employee_id, last_name,department_id

from employees e

order by (select department_name

from departments d

where e.department_id = d.department_id);


相关子查询

相关子查询按照一行接一行的顺序执行,主查询的每一行都执行一次子查询

wKioL1jY0ZPDp46NAABXUTDyPS4665.jpg


子查询中使用主查询中的列

select column1, column2, ...

from table1 Outer_table

where column1 operator

(selecT column1, column2

from table2

where expr1 = Outer_table.expr2);


2、查找所有的员工信息,谁的薪金超过其部门的平均工资

select last_name, salary, department_id

from employees outer_table

where salary >

(selecT AVG(salary)

from employees inner_table

where inner_table.department_id =

outer_table.department_id);


3、显示哪些员工工作变更过至少两次

select e.employee_id, last_name,e.job_id from employees e

where 2 <= (select count(*) from job_history

where employee_id = e.employee_id);


使用 EXISTS 运算符

EXISTS操作符检查在子查询中是否存在满足条件的行。

如果在子查询中存在满足条件的行:

  – 不在子查询中继续查找

  – 条件返回 TRUE

如果在子查询中不存在满足条件的行:

  – 条件返回 FALSE

  – 继续在子查询中查找

1、使用 EXISTS 操作符查找领导

select employee_id, last_name, job_id, department_id

from employees outer

where exists ( select 'x'

from employees

where manager_id =

outer.employee_id);


查找没有任何员工的部门

select department_id, department_name

from departments d

where not exists (select 'x'

from employees

where department_id = d.department_id);


相关UPDATE

使用相关子查询依据一个表中的数据更新另一个表的数据。

update table1 alias1 set column = (select expression from table2 alias2

where alias1.column = alias2.column);


违反范式的表 EMPL6 添加字段存储部门名称(添加字段以后违反范式)

使用相关子更新填充表


alter table empl6 add(department_name varchar2(25));

update empl6 e

set department_name =

(select department_name

from departments d

where e.department_id = d.department_id);


相关DELETE

使用相关子查询依据一个表中的数据删除另一个表的数据

delete from table1 alias1

where column operator

(select expression

from table2 alias2

where alias1.column = alias2.column);


1、使用相关子查询删除EMPL6存在同时也存在于EMP_HISTORY表中的数据。

delete from empl6 e

where employee_id =

(select employee_id

from emp_history

where employee_id = e.employee_id);


WITH 子句

使用 WITH 子句, 可以避免在 SELECT 语句中重复书写相同的语句块

WITH 子句将该子句中的语句块执行一次 并存储到用户的临时表空间中

使用 WITH 子句可以提高查询效率


1、使用WITH子句编写一个查询,来显示部门名称和这些部门员工的工资总额大于跨部门的平均工资的部门及工资总额

with

dept_costs as (

select d.department_name, sum(e.salary) as dept_total

from employees e join departments d

on e.department_id = d.department_id

group by d.department_name),

avg_cost as (

select sum(dept_total)/count(*) as dept_avg

from dept_costs)

select *

from dept_costs

where dept_total >

(select dept_avg

from avg_cost)

order by department_name;


递归 WITH 子句

递归WITH子句:

Enables formulation of recursive queries.

Creates query with a name, called the Recursive WITH element name

Contains two types of query blocks member: anchor and a recursive

Is ANSI-compatible

with reachable_from (source, destin, totalflighttime) as

(

select source, destin, flight_time

from flights

union all

select incoming.source, outgoing.destin,

incoming.totalflighttime+outgoing.flight_time

from reachable_from incoming, flights outgoing

where incoming.destin = outgoing.source

)

select source, destin, totalflighttime

from reachable_from;



本文转自 yuri_cto 51CTO博客,原文链接:http://blog.51cto.com/laobaiv1/1910824,如需转载请自行联系原作者

相关文章
|
4天前
|
SQL 容灾 关系型数据库
阿里云DTS踩坑经验分享系列|DTS打通SQL Server数据通道能力介绍
SQL Server 以其卓越的易用性和丰富的软件生态系统,在数据库行业中占据了显著的市场份额。作为一款商业数据库,外部厂商在通过解析原生日志实现增量数据捕获上面临很大的挑战,DTS 在 SQL Sever 数据通道上深研多年,提供了多种模式以实现 SQL Server 增量数据捕获。用户可以通过 DTS 数据传输服务,一键打破自建 SQL Server、RDS SQL Server、Azure、AWS等他云 SQL Server 数据孤岛,实现 SQL Server 数据源的流动。
51 0
阿里云DTS踩坑经验分享系列|DTS打通SQL Server数据通道能力介绍
|
11天前
|
SQL 关系型数据库 MySQL
基于SQL Server / MySQL进行百万条数据过滤优化方案
对百万级别数据进行高效过滤查询,需要综合使用索引、查询优化、表分区、统计信息和视图等技术手段。通过合理的数据库设计和查询优化,可以显著提升查询性能,确保系统的高效稳定运行。
32 9
|
3月前
|
SQL 数据挖掘 数据库
SQL自学笔记(2):如何用SQL做简单的检索
本文深入介绍了SQL的基本语法,包括数据查询、过滤、排序、分组及表连接等操作,并通过实际案例展示了SQL在用户研究中的应用,如用户行为分析、用户细分、用户留存分析及满意度调查数据分析。
60 0
SQL自学笔记(2):如何用SQL做简单的检索
|
3月前
|
SQL 存储 人工智能
Vanna:开源 AI 检索生成框架,自动生成精确的 SQL 查询
Vanna 是一个开源的 Python RAG(Retrieval-Augmented Generation)框架,能够基于大型语言模型(LLMs)为数据库生成精确的 SQL 查询。Vanna 支持多种 LLMs、向量数据库和 SQL 数据库,提供高准确性查询,同时确保数据库内容安全私密,不外泄。
547 7
Vanna:开源 AI 检索生成框架,自动生成精确的 SQL 查询
|
4月前
|
SQL 存储 缓存
SQL Server 数据太多如何优化
11种优化方案供你参考,优化 SQL Server 数据库性能得从多个方面着手,包括硬件配置、数据库结构、查询优化、索引管理、分区分表、并行处理等。通过合理的索引、查询优化、数据分区等技术,可以在数据量增大时保持较好的性能。同时,定期进行数据库维护和清理,保证数据库高效运行。
121 4
|
5月前
|
SQL 移动开发 Oracle
SQL语句实现查询连续六天数据的方法与技巧
在数据库查询中,有时需要筛选出符合特定时间连续性条件的数据记录
|
5月前
|
SQL 存储 关系型数据库
添加数据到数据库的SQL语句详解与实践技巧
在数据库管理中,添加数据是一个基本操作,它涉及到向表中插入新的记录
|
5月前
|
SQL 数据挖掘 数据库
SQL查询每秒的数据:技巧、方法与性能优化
id="">SQL查询功能详解 SQL(Structured Query Language,结构化查询语言)是一种专门用于与数据库进行沟通和操作的语言
|
5月前
|
SQL
使用SQL进行集合查询和数据维护
使用SQL进行集合查询和数据维护
65 0
|
7月前
|
SQL 存储 开发框架
Entity Framework Core 与 SQL Server 携手,高级查询技巧大揭秘!让你的数据操作更高效!
【8月更文挑战第31天】Entity Framework Core (EF Core) 是一个强大的对象关系映射(ORM)框架,尤其与 SQL Server 数据库结合使用时,提供了多种高级查询技巧,显著提升数据操作效率。它支持 LINQ 查询,使代码简洁易读;延迟加载与预先加载机制优化了相关实体的加载策略;通过 `FromSqlRaw` 或 `FromSqlInterpolated` 方法支持原始 SQL 查询;可调用存储过程执行复杂任务;利用 `Skip` 和 `Take` 实现分页查询,便于处理大量数据。这些特性共同提升了开发者的生产力和应用程序的性能。
337 0

热门文章

最新文章