-- 内连接:
-- 显示员工姓名、工资和公司所在地
  select e.ename, e.sal, d.dname from emp e,dept d; -- 笛卡尔积
  select e.ename, e.sal, d.dname from emp e join dept d; -- oracle语法错误,没有笛卡尔积;mysql 没有语法错误
  select e.ename, e.sal, d.dname from emp e, dept d where e.deptno = d.deptno;
  select e.ename, e.sal, d.dname from emp e join dept d on e.deptno=d.deptno;
-- 显示部门号为10的员工姓名、工资、部门名称
  select d.dname, e.ename, e.sal from emp e, dept d where d.deptno = e.deptno and e.deptno = 10;
  select d.dname, e.ename, e.sal from emp e join dept d on d.deptno = e.deptno where e.deptno = 10;
-- 显示姓名、工资、工资的级别(on 后面不仅仅可以加 ... = ... ,还可以是 between ....and ...)
  select e.ename, e.sal, s.grade from emp e, salgrade s where e.sal between s.losal and s.hisal;
  select e.ename, e.sal, s.grade from emp e join salgrade s on e.sal between s.losal and s.hisal;
-- 显示姓名、工资、部门名称,并按部门号排序
  select e.ename, e.sal, d.dname from emp e join dept d on e.deptno = d.deptno order by e.deptno desc;

-- 自关联
-- 查询FORD上级领导名字
  select e.ename, e.sal, e.job, e.sal, o.ename from emp e join emp o on e.mgr = o.empno where e.ename = 'FORD';

-- 单行子查询:
-- 查询和 SMITH 同一部门的所有员工
  select * from emp e where e.deptno = (select deptno from emp where ename = 'SMITH');
-- 查询工资大于 30号部门所有员工工资的员工
  select * from emp o where o.sal > (select max(sal) from emp e where e.deptno=30);

-- 多列子查询:
-- 查询与 SMITH 的部门和岗位完全相同的员工
  select * from emp e where (e.deptno, e.job) = (select o.deptno, o.job from emp o where o.ename = 'SMITH');
-- 高于自己部门平均工资的员工信息
  select * from emp o join (select deptno, avg(sal) avg_sal from emp group by deptno) e on e.deptno=o.deptno where sal>avg_sal;
-- 修改SIMTH的岗位、工资、补助和scott的一样
  update emp set (job, sal, comm) = (select job, sal, comm from emp where ename='SCOTT') where ename='SMITH'; -- mysql错误

-- 多行子查询

  insert into bonus values(1,2,3,4);
  insert into bonus select * from bonus; -- oracle、mysql都支持
  insert into bonus(ename, job) select ename, job from bonus;
  create table test(id, name, sal, job, deptno) as select empno, ename, sal, job, deptno from emp; -- mysql 错误,用查询结果创建一个新表:

-- 合并查询
-- union, union all, intersect, minus
-- union 并集,去掉重复行
  select * from emp where sal>2500 union select * from emp where job='MANAGER';
-- union all 并集,不去掉重复行
  select * from emp where sal>2500 union all select * from emp where job='MANAGER';
-- intersect 交集
  select * from emp where sal>2500 intersect select * from emp where job='MANAGER';
-- minus 差集,存在于第一个集合中,且不存在于第二个集合中
  select * from emp where sal>2500 minus select * from emp where job='MANAGER'; -- mysql 错误
  select * from emp where job='MANAGER' minus select * from emp where sal>2500; -- mysql 错误

-- 差集图示:

sql 内连接 子查询 合并查询的更多相关文章

  1. SQL - 内连接与外连接

    PDF下载地址:SQL-内连接与外连接.pdf 连接查询在关系型数据库中经常用到,是多表联合查询的基础. 主要包含:内连接,外连接,交叉连接. SQL - 内连接与外连接 内连接 等值连接 不等值连接 ...

  2. My SQL的内连接,外链接查询

    1.内连接:只连接匹配的行. 2.左外连接:包含左边表的全部行,以及右边表中所有匹配的行,无论右边的表有没有和左边匹配的行,左边的所有行都必须要显示. 3.右外连接:包含右边表的全部行,以及左边表中所 ...

  3. oracle 基础SQL语句 多表查询 子查询 分页查询 合并查询 分组查询 group by having order by

    select语句学习 . 创建表 create table user(user varchar2(20), id int); . 查看执行某条命令花费的时间 set timing on: . 查看表的 ...

  4. mysql 子查询 合并查询

    4.1带In 关键字的子查询 一个查询语句的条件可能落在另一个SELECT 语句的查询结果中. SELECT * FROM t_book WHERE booktypeId IN (SELECT id ...

  5. sql 内连接和外链接

    如表     -------------------------------------------------     table1 | table2 |     ----------------- ...

  6. SQL内连接-外连接join,left join,right join,full join

    1.创建测试表test1及test2 SQL)); 表已创建. SQL)); 表已创建. ,'name1'); ,'name2'); ,'name3'); ,'name4'); ,'name5'); ...

  7. sql 内连接、外连接、自然连接等各种连接

    1.内联接(典型的联接运算,使用像 = 或 <> 之类的比较运算符).包括相等联接和自然联接. 内联接使用比较运算符根据每个表共有的列的值匹配两个表中的行.例如,检索 students和c ...

  8. SQL内连接与外连接的区别【转】

    --表stuid name 1, Jack2, Tom3, Kity4, nono--表examid grade1, 562, 7611, 89 内连接 (显示两表id匹配的)select stu.i ...

  9. sql内连接外连接自然连接

    为什么我们要使用内连接和外连接呢?可以从两张或者多张表中找出,我们需要的属性. 这个比较好:http://www.cnblogs.com/youzhangjin/archive/2009/05/22/ ...

随机推荐

  1. 2018-2019-2 20165313 《网络对抗技术》 Exp7:网络欺诈防范

    一.实践内容(3.5分) 本实践的目标理解常用网络欺诈背后的原理,以提高防范意识,并提出具体防范方法.具体实践有 (1)简单应用SET工具建立冒名网站 (1分) (2)ettercap DNS spo ...

  2. (转)AIX光盘备份与恢复

    AIX光盘备份与恢复 在此之前,说明一下光盘映像的格式UDF和ISO9660 ISO9660: 这是国际标准化组织(ISO)于1985年颁布的通用光盘文件系统.目前使用最广泛的光盘文件系统,能被所有的 ...

  3. 使用seek()方法报错:“io.UnsupportedOperation: can't do nonzero cur-relative seeks”错误的原因

    在使用seek()函数时,有时候会报错为  “io.UnsupportedOperation: can't do nonzero cur-relative seeks”,代码如下: >>& ...

  4. 【Java并发编程】:线程中断

    使用interrupt()中断线程 当一个线程运行时,另一个线程可以调用对应的Thread对象的interrupt()方法来中断它,该方法只是在目标线程中设置一个标志,表示它已经被中断,并立即返回.这 ...

  5. win7,8走网络打印机出现删除设备和打印机门未关闭的解决方法

    不多说,直接上干货! 用学校的内网连接, 即可. 右键,查看设备网页. 出现下面的情况: 多学学. 欢迎大家,加入我的微信公众号:大数据躺过的坑        人工智能躺过的坑       同时,大家 ...

  6. NodeJS require a global module/package in linux

    https://stackoverflow.com/questions/15636367/nodejs-require-a-global-module-package 1  export NODE_P ...

  7. Django之模型系统

    Django模型简介 Django 模型是与数据库相关的,与数据库相关的代码一般写在 models.py 中 Django 支持 sqlite3, MySQL, oracle,PostgreSQL等数 ...

  8. Go语言学习笔记五: 条件语句

    Go语言学习笔记五: 条件语句 if语句 if 布尔表达式 { /* 在布尔表达式为 true 时执行 */ } 竟然没有括号,和python很像.但是有大括号,与python又不一样. 例子: pa ...

  9. Maven Source Plugin

    项目pom文件build下添加配置: 01 <plugin> 02 <groupId>org.apache.maven.plugins</groupId> 03 & ...

  10. ABP实战--分页排序

    待完成... public async Task<DatatablesResultDto<TaskDto>> GetList(KeywordDatatablesRequestD ...