先决条件 – 连接(内部、左、右和完全连接)
1. 自然连接:
自然连接基于相同的属性名称和数据类型连接两个表。结果表将包含两个表的所有属性,但只保留每个公共列的一个副本。
例子:
考虑下面给出的两个表:
考虑给定的查询
SELECT *
FROM Student S NATURAL JOIN Marks M;
输出:
2. 内连接:
内连接基于在 ON 子句中明确指定的列连接两个表。结果表将包含两个表中的所有属性,还包括公共列。
例子:
考虑以上两个表,查询如下:
SELECT *
FROM student S INNER JOIN Marks M ON S.Roll_No = M.Roll_No;
输出 :
SQL 中 Natural JOIN 和 INNER JOIN 的区别:
SR.NO. | NATURAL JOIN | INNER JOIN |
---|---|---|
1. | Natural Join joins two tables based on same attribute name and datatypes. | Inner Join joins two table on the basis of the column which is explicitly specified in the ON clause. |
2. | In Natural Join, The resulting table will contain all the attributes of both the tables but keep only one copy of each common column | In Inner Join, The resulting table will contain all the attribute of both the tables including duplicate columns also |
3. | In Natural Join, If there is no condition specifies then it returns the rows based on the common column | In Inner Join, only those records will return which exists in both the tables |
4. | SYNTAX: SELECT * FROM table1 NATURAL JOIN table2; |
SYNTAX: SELECT * FROM table1 INNER JOIN table2 ON table1.Column_Name = table2.Column_Name; |