SQL子查询全接触(2)
作者:dnawo 发布时间:2007-08-20 10:51:00
标签:sql,查询,子查询
二、子查询类型
1.带in的嵌套查询
select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal in (select sal from scott.emp where ename=’’WARD’’);
上述语句完成的是查询薪水和WARD相等的员工,也可以使用not in来进行查询。
2.带any的嵌套查询
select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal >any(select sal from scott.emp where job=’’MANAGER’’);
等价于下边两步的执行过程:
(1)执行“select sal from scott.emp where job=’’MANAGER’’”
(2)查询到3个薪水值2975、2850和2450,父查询执行下列语句:
select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal >2975 or sal>2850 or sal>2450;
3.带some的嵌套查询
select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal =some(select sal from scott.emp where job=’’MANAGER’’);
等价于下边两步的执行过程:
(1)子查询,执行“select sal from scott.emp where job=’’MANAGER’’”。
(2)父查询执行下列语句。
select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal =2975 or sal=2850 or sal=2450;
带【any】的嵌套查询和【some】的嵌套查询功能是一样的。早期的SQL仅仅允许使用【any】,后来的版本为了和英语的【any】相区分,引入了【some】,同时还保留了【any】关键词。
4.带all的嵌套查询
select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal >all(select sal from scott.emp where job=’’MANAGER’’);
等价于下边两步的执行过程:
(1)子查询,执行“select sal from scott.emp where job=’’MANAGER’’”。
(2)父查询执行下列语句。
select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal >2975 and sal>2850 and sal>2450;
5.带exists的嵌套查询
select emp.empno,emp.ename,emp.job,emp.sal from scott.emp,scott.dept where exists (select * from scott.emp where scott.emp.deptno=scott.dept.deptno);
6.并操作的嵌套查询
并操作就是集合中并集的概念。属于集合A或集合B的元素总和就是并集。
(select deptno from scott.emp) union (select deptno from scott.dept);
7.交操作的嵌套查询
交操作就是集合中交集的概念。属于集合A且属于集合B的元素总和就是交集。
(select deptno from scott.emp) intersect (select deptno from scott.dept);
8.差操作的嵌套查询
差操作就是集合中差集的概念。属于集合A且不属于集合B的元素总和就是差集。
(select deptno from scott.dept) minus (select deptno from scott.emp);
并、交和差操作的嵌套查询要求属性具有相同的定义,包括类型和取值范围。


猜你喜欢
- 一、概述SQL Server提供了“分离/附加”数据库、“备份/还原”数据库、复制数据库等多种数据库的备份和恢复方法。这里介绍一种学习中常用
- 上次亚马逊的商品信息都获取到了,自然要看一下评论的部分。用户的评论能直观的反映当前商品值不值得购买,亚马逊的评分信息也能获取到做一个评分的权
- 来源:Chinaasp 之所以翻译这篇文章,是因为目前关于CGI安全性的文章都是拿Perl作为例子,而专门介绍ASP,PHP或者JSP安全性
- 众所周知,pip 可以安装、更新、卸载 Python 的第三方库,非常方便。你们中的许多人可能已经使用 pip 
- 这个主要应用于,获取用户输入的时候,防止用户不小心,多输入了一个空格,导致验证无法通过,多用于用户名跟密码的,好多情况下,大家复制的winr
- 废话不多说,直接上代码Python2.7#!/usr/bin/env python2.7# -*- coding=utf-8 -*-impo
- 如何在读取Excel文件时创建列表的下拉菜单?代码如下,用来创建工作表列表的下拉菜单: < select 
- 目录实践步骤完整代码参考实践步骤1.寻找合适的Python库(安装是否麻烦、使用是否简便、执行会不会太久)moviepy 音视频库。分析需要
- 今天下载wordcloud的时候出现了很多问题,在此总结总结1.问题一:You are using pip version 19.0.3,
- 1. 安装依赖pip install pypiserver passlib2. 创建packages目录mkdir packages3. 以
- 引用:Art Kulakov 《How to Build a Python Interpreter Inside ChatGPT》这个灵感来
- python中查找指定的字符串的方法如下:code#查询def selStr(): sStr1 = 'jsjtt.com
- 某次一不小心,用了delete from xxx 删除了几条重要数据,在网上找了很多方法,但都比较零散,打算记录本次数据找回的过程。大致分为
- 前言之前写过一篇关于阿里的低代码工具LowCodeEngine 的文章,发现大家还是挺感兴趣的。最近又发现了一款很有意思的低代码工具Yao,
- 元编程,一个听起来特别酷的词,强大的Lisp在这方面是好手,对于Python,尽管没有完善的元编程范式,一些天才的开发者还是创作了很多元编程
- 1.背景 sysbench是一款压力测试工具,可以测试系统的硬件性能,也可以用来对数据库进行基准测试。sysbench 支持的测试
- 本文实例为大家分享了python3实现简单飞机大战的具体代码,供大家参考,具体内容如下游戏分为两个部分:1.主程序 2.游戏工具主程序实现:
- 本文为大家分享了数据库优化方案,供大家参考,具体内容如下1. 利用表分区分区将数据在物理上分隔开,不同分区的数据可以制定保存在处于不同磁盘上
- TensorFlow保存模型代码import tensorflow as tffrom tensorflow.python.framewor
- python中可以使用下标索引来访问列表中的值,对列表进行切片即截取,也可以对列表的数据项进行修改或更新。使用下标索引来访问列表中的值,例如