`

SQL: JOIN

阅读更多
http://superman-chenzs.itpub.net/post/29327/276049

笛卡尔积:
cross join

Join Types:
1. Inner Join
- Equal-Join
- Non-Equal-Join
2. Outer Join
- Left-Outer-Join
- Right-Outer-Join
3. Self Join
- Join a table to itself
- Using the table's alias name to complete the self join.
- attention the repeat lines
-----------------------------------------------------------------------------------------------------------------
Joining Tables Using Oracle Syntax:
- Write the join condition in the WHERE clause.
- Prefix the column name with the table name when the same column name appears in more than one table.
- Additional Search Conditions Using the AND Operator
- To join n tables together,you need a minimun of n-1 join conditions.

-----------------------------------------------------------------------------------------------------------------
Qualifying Ambiguous Column Names:
- Use table prefixes to qualify column names that are in multiple tables.
- Improve performance by using table prefixes.
- Distinguish columns that have identical names but reside in different tables by using column aliases.

-----------------------------------------------------------------------------------------------------------------
Outer Join:
- You use an outer join to also see rows that do not meet the join condition.
- The Outer join operator is the plus sign (+).
- The sign '(+)' is on the ritht,the type is Left-Outer-Join
- The sign '(+)' is on the left ,the type is Right-Outer-Join

eg:
----------------------------------
the Right-Outer-Join:
--->
select empno,ename,d.deptno
from emp e,dept d
where e.deptno(+) = d.deptno

EMPNO ENAME DEPTNO
---------- ---------- ----------
7369 SMITH 20
7499 ALLEN 30
7521 WARD 30
7566 JONES 20
7654 MARTIN 30
7698 BLAKE 30
7782 CLARK 10
7788 SCOTT 20
7839 KING 10
7844 TURNER 30
7876 ADAMS 20
7900 JAMES 30
7902 FORD 20
7934 MILLER 10
40
----------------------------------
the Left-Outer-Join:
--->
select empno,ename,d.deptno
from emp e, dept d
where e.deptno = d.deptno(+);

EMPNO ENAME DEPTNO
--------- ---------- ----------
7369 SMITH 20
7499 ALLEN 30
7521 WARD 30
7566 JONES 20
7654 MARTIN 30
7698 BLAKE 30
7782 CLARK 10
7788 SCOTT 20
7839 KING 10
7844 TURNER 30
7876 ADAMS 20
7900 JAMES 30
7902 FORD 20
7934 MILLER 10



**********************************************************************************************************************
Joining Tables Using SQL: 1999 Syntax
Syntax:
SELECT table1.column,table2.column
FROM table1
[CROSS JOIN table2] |
[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)] ;

-----------------------------------------------------------------------------------------------------------------------
Creating Cross Joins:
- The CROSS JOIN clause produces the cross-product of two tables.
- This is the same as a Cartesian product between the two tables.(迪卡尔集)
eg:
select empno,ename,d.deptno from emp e CROSS JOIN dept d;
===
select empno,ename,d.deptno from emp e,dept d;

-----------------------------------------------------------------------------------------------------------------------
Creating Natural Joins:
- The NATURAL JOIN clause is based on all columns in the two tables that have the same name.
- It selects rows from the two tables that have equal values in all matched columns.
- If the columns having the same names have different data types,an error is returned.
- Do not use a table name or alias in the referenced columns.
eg:
select empno,ename,deptno from emp NATURAL JOIN dept;
===
SELECT empno,ename,d.deptno from emp e,dept d where e.deptno=d.deptno;

-----------------------------------------------------------------------------------------------------------------------
Creating Join with the USING Clause:
- If several columns have the same names but the data types do not match,the NATURAL JOIN clause can be modified with the USING clause to specify the columns that should be used for an equijoin.
- Use the USING clause to match only one column when more than one column matches.
- Do not use a table name or alias in the referenced columns.
- The NATURAL JOIN and USING clauses are mutually exclusive(相互排斥).

-----------------------------------------------------------------------------------------------------------------------
Creating Joins with the ON Clause:
- The join condition for the natural join is basically an equijoin of all columns with the same name.
- To specify arbitrary(任意的) conditions or specify columns to join ,the ON clause is used.
- The join condition is separated from other search conditions.
- The ON clause makes code easy to understand.
eg:
select e.empno,e.ename,d.deptno from emp e join dept d on e.deptno=d.deptno;
===
select e.empno,e.ename,d.deptno from emp e ,dept d where e.deptno=d.deptno;
- Creating Three-Way Joins with the ON Clause,eg:
------
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;
- that means N JOINS must have N-1 join_conditions.

-----------------------------------------------------------------------------------------------------------------------
INNER JOIN & OUTER JOIN:
- In SQL:1999, the join fo two tables returning only matched rows is an inner join.
- A join between two tables that returns the results of the inner join as well as unmatched rows left (or right) tables is a left (or right) outer join.
- A join between two tables that returns the results of an inner join as well as the results of a left and right join is a full outer join.
- The Left-Outer-Join: show all the value in the leftside table, even the rightside's value is null.
- The Right-Outer-Join: show all the value in the rightside table, even the leftside's value is null.
--------
eg:
--------
LEFT JOIN:
select ename,d.deptno from emp e left outer join dept d ON e.deptno=d.deptno;
===
select ename,d.deptno from emp e ,dept d where e.deptno=d.deptno(+);
ENAME DEPTNO
---------- ----------
SMITH 20
ALLEN 30
WARD 30
JONES 20
MARTIN 30
BLAKE 30
CLARK 10
SCOTT 20
KING 10
TURNER 30
ADAMS 20
JAMES 30
FORD 20
MILLER 10

已选择14行
--------
RIGHT JOIN:
select ename,d.deptno from emp e RIGHT OUTER JOIN dept d ON e.deptno=d.deptno;
===
select ename,d.deptno from emp e,dept d where e.deptno(+)=d.deptno;
ENAME DEPTNO
---------- ----------
SMITH 20
ALLEN 30
WARD 30
JONES 20
MARTIN 30
BLAKE 30
CLARK 10
SCOTT 20
KING 10
TURNER 30
ADAMS 20
JAMES 30
FORD 20
MILLER 10
40

已选择15行
--------
FULL OUTER JOIN:
select ename,d.deptno from emp e FULL OUTER JOIN dept d ON e.deptno=d.deptno;
ENAME DEPTNO
---------- ----------
SMITH 20
ALLEN 30
WARD 30
JONES 20
MARTIN 30
BLAKE 30
CLARK 10
SCOTT 20
KING 10
TURNER 30
ADAMS 20
JAMES 30
FORD 20
MILLER 10
40

分享到:
评论

相关推荐

    Oracle SQL:经典查询练手四篇

    Oracle SQL是数据库管理中不可或缺的一部分,它用于在Oracle数据库中执行数据查询和操作。本系列“Oracle SQL:经典查询练手四篇”旨在通过一系列实际的查询示例,帮助用户提高对SQL语言的理解和应用能力。 在第一...

    SQL语句left join/right join/inner join 的用法比较

    在SQL查询中,JOIN操作是连接两个或多个表的关键部分,用于从这些表中提取相关数据。本篇文章将深入探讨LEFT JOIN、RIGHT JOIN以及INNER JOIN的用法,并通过实例进行对比,帮助理解它们之间的差异。 1. LEFT JOIN...

    JsonSQL:用SQL语句解析JSON文件

    4. **JOIN**: 尽管JSON结构不支持传统SQL的表连接,但JsonSQL可以通过嵌套查询实现类似的效果。例如,如果你有包含用户和其订单的JSON,可以使用嵌套查询来关联用户和他们的订单。 5. **GROUP BY** 和 **HAVING**: ...

    SQL 连接 JOIN 例解

    SQL 连接 JOIN 例解

    SQL中JOIN的使用详解

    ### SQL中JOIN的使用详解 在SQL查询语言中,`JOIN`操作是非常重要的一个部分,它主要用于合并两个或多个表中的数据。通过不同的`JOIN`类型,我们可以灵活地获取所需的组合数据。本文将详细介绍五种主要的`JOIN`类型...

    SQL left join

    * Right Join:以右表为基础,返回右表中的所有记录和左表中符合条件的记录。 * Inner Join:返回两个表中符合条件的记录,不以任何一方为基础。 例如,使用Right Join语句: ```sql SELECT * FROM A RIGHT JOIN B ...

    LINQ to SQL在行动:Join和Group-学习在.NET程序中编写数据访问功能的方法.pdf

    标题《LINQ to SQL在行动:Join和Group-学习在.NET程序中编写数据访问功能的方法》指出了本文的核心内容,即探讨如何利用LINQ to SQL这一技术在.NET环境下实现数据访问功能,并详细解析了如何使用Join和Group操作来...

    SQL 之JOIN 用法完全版.rar

    在SQL(Structured Query Language)中,JOIN操作是用于合并两个或更多个表的数据,以便根据它们之间的关联性创建更复杂的查询结果。JOIN是数据库查询中的关键部分,它允许我们在多个表之间建立联系,从而获取更丰富...

    SQL.zip_sql:查询一个人

    总之,查询一个人的信息在SQL中涉及到对多个表的联合查询,通过`JOIN`操作连接这些表,并使用`WHERE`子句来筛选特定的用户。随着业务需求的复杂性增加,查询语句也可能需要包含更多的功能,如分组、排序、过滤等。...

    SQL join 完全用法

    SQL join 完全用法 SQL join 完全用法 学习sql join

    【SQL】在sql server中 delete时 使用INNER JOIN

    ### SQL Server 中 DELETE 语句结合 INNER JOIN 的应用 #### 背景介绍 在数据库管理与维护过程中,经常会遇到需要删除表中的某些记录的情况。简单地使用 `DELETE` 语句可以删除单个表中的数据,但在多表关联的情况...

    sql语句中join的用法

    ### SQL语句中JOIN的用法详解 在SQL查询语言中,`JOIN`是一个非常重要的概念,它允许我们从两个或多个表中提取数据。通过使用不同类型的JOIN操作,我们可以根据表之间的关系来灵活地组织数据。下面我们将详细介绍几...

    LINQ to SQL语句之Join和Order By

    LINQ to SQL语句之Join和Order By部分代码 语句描述:这个例子在From子句中使用外键导航筛选在西雅图的雇员,同时列出其所在地区。这条生成SQL语句为: SELECT [t0].[FirstName], [t0].[LastName], [t2]....

    SQL中的left outer join,inner join,right outer join用法详解

    * LEFT OUTER JOIN:一个表满足条件的行,和另一个表的所有行。 * RIGHT OUTER JOIN:与 LEFT 相同,但两个表的角色互换。 * FULL OUTER JOIN:LEFT OUTER 和 RIGHT OUTER 中所有行的超集。 内连接(Inner Join) ...

    Oracle SQL:经典练习题(附答案)

    Oracle SQL是数据库管理和数据分析的重要工具,尤其在处理Oracle数据库时不可或缺。本文提供的经典练习题旨在帮助初学者熟悉SQL语句的编写,特别是针对Oracle数据库特有的功能。以下将详细讲解涉及的知识点。 1. **...

    PL/SQL Join实例分析

    在PL/SQL(Oracle数据库的编程语言)中,JOIN语法尤其重要,因为它是处理复杂查询的基础。以下是PL/SQL中不同类型的JOIN及其特点的详细分析: 1. **左连接(LEFT JOIN)**: 左连接返回左表(Aa)的所有行,即使在...

    sql中join总结

    根据不同的需求,SQL提供了多种类型的JOIN,包括交叉JOIN、内JOIN、左外JOIN、右外JOIN和全外JOIN。以下是对这些JOIN类型的具体解释和示例。 1. **交叉JOIN (Cross JOIN)**: 交叉JOIN也称为笛卡尔积,它返回第一个...

    SQL:经典语句大全

    ### SQL经典语句详解 #### 一、数据库操作 **1. 创建数据库** SQL语言提供了`CREATE DATABASE`语句来创建一个新的数据库。这通常是在项目启动或数据库环境搭建初期执行的操作。 ```sql CREATE DATABASE ...

    Oracle SQL:经典查询练手

    Oracle SQL是一种强大的数据库查询语言,尤其在处理结构化数据时,它的功能和灵活性深受数据库管理员和开发人员的喜爱。本主题将深入探讨Oracle SQL中的经典查询技巧,帮助你提升数据库操作的效率。 首先,理解SQL...

    sql的 INNER JOIN 语法

    ### SQL的INNER JOIN语法 #### 一、概念与作用 在SQL语言中,`INNER JOIN`是一种连接(Join)操作,用于从两个或多个表中提取数据,仅返回那些匹配的记录。简单来说,它会查找两个表中具有共同属性(如相同的键值...

Global site tag (gtag.js) - Google Analytics