-- 内连接:
-- 显示员工姓名、工资和公司所在地
  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. sql子查询在insert、update、delete中的应用

    子查询可以应用在INSERT.UPDATE以及DELETE等语句中,合理的使用子查询将能够简化功能的实现并且极大的提高SQL语句执行的效率 用到的表: CREATE TABLE `t_readerfa ...

  2. artTemplate-优秀的前端模板引擎

    artTemplate-优秀的前端模板引擎 1.html中引入artTemplate的js文件: <script type="text/javascript" src=&qu ...

  3. 【链表】Add Two Numbers

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  4. 【原】Ubuntu13.04安装、卸载Gnome3.8

    添加 GNOME 3 PPA(Personal Package Archives) 我们先给Ubuntu的Software Sources增加GNOME 3 PPA.这可以不用命令行就做到,但出于简单 ...

  5. 15-hadoop-eclipse插件的安装

    好久没更新了, 也好久没学了, 今天换了个eclipse版本, 安装插件坑了一会, 果然好记性不如烂笔头, 记下来吧 编译安装或者直接安装都可以, 先说下编译安装吧 1, 编译安装, 是使用的ant, ...

  6. kubernetes continually evict pod when node's inode exhausted

    kubernetes等容器技术可以将所有的业务进程运行在公共的资源池中,提高资源利用率,节约成本,但是为避免不同进程之间相互干扰,对底层docker, kubernetes的隔离性就有了更高的要求,k ...

  7. ASP.NET 之 MVC框架及搭建

    一.MVC简介 MVC:Model-View-Controller(模型-视图-控制器),MVC是一种软件开发架构模式. 1.模型(Model) 模型对象是实现应用程序数据域逻辑的应用程序部件. 通常 ...

  8. C# 枚举转字符串

    有时候需要把枚举转字符串,那么如何把枚举转字符串? 枚举转字符串 假如需要把枚举转字符串,可以直接把他进行转换,请看代码 public enum Di { /// <summary> // ...

  9. webfrom后台

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  10. MyBatis --- 配置步骤

    本文并非具体的细节,而是主要的配置步骤 概述 MyBatis 是半自动的ORM 框架,在MyBatis 整合 Spring Boot 的时候步骤比较繁琐,所以写下此篇纪录一下步骤. 使用 MyBati ...