使用连接
SQL 1999
SELECT table1.column, table2.column
FROM table1
[NATURAL JOIN table2] |
[JOIN table2 USING (column_name)] |
[JOIN table2
ON (table1.column_name = table2.column_name)]|
[LEFT|RIGHT|FULL OUTER JOIN table2
ON (table1.column_name = table2.column_name)]|
[CROSS JOIN table2];

自然连接:
SELECT department_id, department_name, location_id, city
FROM departments NATURAL JOIN locations ;

select department_id, department_name,department.location_id, city
from departments, locations
where departments.location_id = locations.location_id

create table t1(c1 number, c2 number, c3 number, c4 number);
create table t2(c1 number, c2 number, c3 number);
insert into t1 values(1,1,1,1);
insert into t1 values(2,2,11,11);
insert into t1 values(3,3,3,4);
insert into t2 values(1,11,11);
insert into t2 values(1,11,11);
insert into t2 values(2,2,111);
insert into t2 values(2,2,111);
insert into t2 values(3,3,3);
insert into t2 values(3,3,3);
select * from t1 natural join t2;
select t1.c1,t1.c2,t1.c3 from t1,t2
where t1.c1 = t2.c1
and t1.c2 = t2.c2
and t1.c3 = t2.c3

using子句
select * from t1 join t2 using(c1);
select * from t1 join t2 using(c1,c2);
oracle语法
select t1.c1,t1.c2,t1.c3 from t1,t2 where t1.c1 = t2.c1
select t1.c1,t1.c2,t1.c3 from t1,t2 where t1.c1 = t2.c1 and t1.c2 = t2.c2;

SELECT employee_id, last_name,
location_id, department_id
FROM employees JOIN departments
USING (department_id) ;

using列出现在别的位置,不能用别名限定
SELECT l.city, d.department_name
FROM locations l JOIN departments d
USING (location_id)
WHERE d.location_id = 1400; (Not OK)

SELECT l.city, d.department_name
FROM locations l JOIN departments d
USING (location_id)
WHERE location_id = 1400; (OK)

on子句
SELECT e.employee_id, e.last_name, e.department_id,
d.department_id, d.location_id
FROM employees e JOIN departments d
ON (e.department_id = d.department_id);

SELECT e.employee_id, e.last_name, department_id, d.location_id
FROM employees e JOIN departments d
USING (department_id);

SELECT employee_id, city, department_name
FROM employees e
JOIN departments d
ON d.department_id = e.department_id
JOIN locations l
ON d.location_id = l.location_id;

oracle语法
SELECT employee_id, city, department_name
FROM employees e,departments d, locations l
WHERE d.department_id = e.department_id
AND d.location_id = l.location_id;

在join中应用额外条件
SELECT e.employee_id, e.last_name, e.department_id,
d.department_id, d.location_id
FROM employees e JOIN departments d
ON (e.department_id = d.department_id)
AND e.manager_id = 149 ;

SELECT e.employee_id, e.last_name, e.department_id,
d.department_id, d.location_id
FROM employees e JOIN departments d
ON (e.department_id = d.department_id)
WHERE e.manager_id = 149 ;

自连接:
SELECT worker.last_name emp, manager.last_name mgr
FROM employees worker JOIN employees manager
ON (worker.manager_id = manager.employee_id);

非等值连接
select ename, sal, grade
from emp join salgrade
on (sal between losal and hisal);

select ename, sal, grade
from emp,salgrade
where sal between losal and hisal;

外连接
create table e as select * from emp;
create table d as select * from dept;
select distinct deptno from e;
select deptno from d;
select ename, e.deptno,d.deptno from e join d on (e.deptno = d.deptno);
select ename, e.deptno,d.deptno from e right join d on (e.deptno = d.deptno);
select ename, e.deptno,d.deptno from e,d where e.deptno(+) = d.deptno;

insert into e values(8888,'ZhangHua','salesman',7698,'08-SEP-87',800,0, 50);
select ename, e.deptno,d.deptno from e left join d on (e.deptno = d.deptno);
select ename, e.deptno,d.deptno from e,d where e.deptno = d.deptno(+);

交叉连接(笛卡尔积)
SELECT ename,dname FROM emp cross join dept;
SELECT ename,dname FROM emp,dept;

Oracle SQL Lesson (6) - 使用Join进行联合查询的更多相关文章

  1. SQL Server中INNER JOIN与子查询IN的性能测试

    这个月碰到几个人问我关于"SQL SERVER中INNER JOIN 与 IN两种写法的性能孰优孰劣?"这个问题.其实这个概括起来就是SQL Server中INNER JOIN与子 ...

  2. Oracle SQL——inner jion;left join;right join的区别和使用场景

    背景 在一次面试的时候,面试官让我说一下这三者的使用场景和区别,当时瞬间懵逼,哈哈.回来赶快看一看,记下来. 详解 inner join 等值查询:返回两张表中,联结字段值相等的组合记录 举例:所有学 ...

  3. Oracle SQL Lesson (9) - 操作数据(增删改)

    使用INSERT语句INSERT INTO table [(column [, column...])]VALUES (value [, value...]); INSERT INTO departm ...

  4. Oracle SQL Lesson (2) - 限制和排序数据

    重建scott用户@?/rdbms/admin/utlsampl.sql@--执行?--$ORACLE_HOME 字符区分大小写:SELECT last_name, job_id, departmen ...

  5. Oracle SQL Lesson (1) - 使用SQL Select语句获取数据

    第一节课: 启动数据库并且使用特定用户连接:su - oracle; 启动sqlplus并且使用sys连接:conn / as sysdba; 启动数据库:startup; 解锁用户:alter us ...

  6. Oracle SQL Lesson (3) - 使用单行函数自定义输出

    大小写转换函数LOWER('SQL Course') = sql courseUPPER('SQL Course') = SQL COURSEINITCAP('SQL Course') = Sql C ...

  7. Oracle SQL Lesson (4) - 使用转换函数和条件表达式

    隐式转换select * from emp where empno='7788'字符(char,varchar2)转换为数字(number)或日期(date)数字或日期转换为字符 显式转换字符转换为数 ...

  8. Oracle SQL Lesson (11) - 创建其他数据库对象(试图/序列/索引/同义词)

    schema(模式)一个用户下一组对象的集合,一般与用户名一致. 视图 CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view [(alias[, alias].. ...

  9. Oracle SQL Lesson (5) - 使用组函数输出聚合数据

    组函数AVGCOUNTMAXMINSUMVARIANCE:方差STDDEV:标准差 SELECT AVG(salary), MAX(salary), MIN(salary), SUM(salary)F ...

随机推荐

  1. 多个haproxy 之间跳转

    C:\>ping wechatTest.winfae.com 正在 Ping wechatTest.winfae.com [120.55.118.6] 具有 32 字节的数据: 来自 120.5 ...

  2. 【Demo 0002】Java基础-语句

    本章学习要点:        1.  掌握Java关健语句使用方法;          2.  理解与语句相关的关键字用法; 一.Java 关键语句        Java语句以及关联关键字与C完全相 ...

  3. Python Errors and Exceptions

    1. python中的try{}catch{} 2. raise exception 3. try...except ... else.. 4. finally块 python中的异常处理的keywo ...

  4. CodeForces Round #191 (327C) - Magic Five 等比数列求和的快速幂取模

    很久以前做过此类问题..就因为太久了..这题想了很久想不出..卡在推出等比的求和公式,有除法运算,无法快速幂取模... 看到了 http://blog.csdn.net/yangshuolll/art ...

  5. linux串口编程总结

    串口本身.标准和硬件 † 串口是计算机上的串行通讯的物理接口.计算机历史上,串口以前被广泛用于连接计算机和终端设备和各种外部设备.尽管以太网接口和USB接口也是以一个串行流进行数据传送的.可是串口连接 ...

  6. 在 Ubuntu 12.04 上通过安装源安装 Open vSwitch (OVS)

    先把Ubuntu 12.04更新一下 sudo apt-get update sudo apt-get upgrade sudo apt-get dist-upgrade 删除 Ebtables包 s ...

  7. 14.4.2 Configuring InnoDB for Read-Only Operation 配置InnoDB 永于只读操作:

    14.4.2 Configuring InnoDB for Read-Only Operation 配置InnoDB 永于只读操作: 你可以查询InnoDB 表 MySQL 数据目录是在只读介质里,通 ...

  8. [置顶] 大量相关gis资源网盘打包下载

    详细请下载附件 所有资源下载(网盘下载): http://laoheitan.bego.cc arcgis教程: http://www.bego.cc/file/23322579 ENVI教程: ht ...

  9. js技术要点---JS 获取网页源代码

    JS 获取网页源代码 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html& ...

  10. 让你提前知道软件开发(22):shell脚本文件操作

    文章1部分 再了解C语言 shell脚本中的文件操作 [文章摘要] 编写shell脚本时,经常会涉及到对文件的操作,比方从文件里读取一行数据.向文件追加一行数据等. 完毕文件读写操作的方法有非常多,了 ...