-- 内连接:
-- 显示员工姓名、工资和公司所在地
  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. IntelliJ IDEA使用心得之非Maven项目篇

    今天和大家分享下非Maven项目在IDEA中的配置方法,由于非Maven项目的配置方法基本相同,所以此篇只对不同点进行说明. 1.声明依赖库 我们可以使用库的方式来添加项目依赖,这是一个非常好的实践. ...

  2. C++中虚函数的动态绑定和多态性

    目录 静态类型 vs 动态类型.静态绑定 vs 动态绑定 虚函数动态绑定实现机制.虚析构函数 多态性 一.静态 vs 动态 静态类型 VS 动态类型.静态类型指的是对象声明的类型,在编译器确定的.动态 ...

  3. 使用vue2+Axios+Router 之后的总结以及遇到的一些坑

    构建 vue有自己的脚手架构建工具vue-cli,使用起来非常方便,使用webpack来集成各种开发便捷工具,比如: 代码热更新,修改代码之后网页无刷新改变,对前端开发来说非常的方便 PostCss, ...

  4. 纯Java JDBC连接数据库,且用JDBC实现增删改查的功能

    Java JDBC连接数据库 package cn.cqvie.yjq; import java.sql.*; /** * 注册数据库的驱动程序,并得到数据库的连接对象 * @author yu * ...

  5. JavaScript深浅拷贝

    深浅拷贝 基本类型和引用类型 ECMAScript 中的变量类型分为两类: 基本类型:undefined,null,布尔值(Boolean),字符串(String),数值(Number) 引用类型: ...

  6. unity游戏热更新总结

    1.利用反射来做Dll更新 这种方式只支持windows以及安卓这种支持JIT的平台,对于IOS就不适用了,IOS这种Full-AOT的平台不支持生成新的代码,因此这种热更方式很少用到.   2.利用 ...

  7. Golang cron 定时任务使用

    1.cron 表达式的基本格式 用过 linux 的应该对 cron 有所了解.linux 中可以通过 crontab -e 来配置定时任务.不过,linux 中的 cron 只能精确到分钟.而我们这 ...

  8. bootstrap table分页,重新数据查询时页码为当前页问题

    问题描述: 使用bootstrap table时遇到一个小问题,第一次查询数据未5页,翻页到第5页后,选中条件再次查询数据时,传到后端页码仍旧为5,而此时数据量小于5页,表格显示为未查询到数据. 处理 ...

  9. MVC页面缓存

    1.OutputCache 属性 contact.cshtml    [OutputCache(Duration=10)] public ActionResult Contact()   {      ...

  10. 008.在C#中,显式接口VS隐式接口

    原文http://www.codeproject.com/Articles/1000374/Explicit-Interface-VS-Implicit-Interface-in-Csharp (At ...