Вы находитесь на странице: 1из 3

SQL Joins

Types of SQL join: Inner join Equi-join o Natural join o Cross join Outer joins o Left outer join o Right outer join o Full outer join Self-join Inner Join: The INNER JOIN keyword returns rows when there is at least one match in both tables. Syntax: Inner join syntax is as follows: SELECT <column list> FROM <left joined table> INNER JOIN <right joined table> ON <join condition> Example: Select ename,mgr, hiredate, dept.dname, dept.loc from emp INNER JOIN dept on emp.deptno = dept.deptno; Equi-join: Natural join: The SQL NATURAL JOIN is a type of equi-join and is structured in such a way that, columns with same name of associate tables will appear once only. Syntax: Natural join syntax is as follows: SELECT <column list> FROM <left joined table> NATURAL JOIN <right joined table> ON <join condition> Example: Select ename,mgr, hiredate, dept.dname, dept.loc from emp NATURAL JOIN dept on emp.deptno = dept.deptno;

Cross join: The SQL CROSS JOIN produces a result set which is the number of rows in the first table multiplied by the number of rows in the second table, if no WHERE clause is used along with CROSS JOIN. This kind of result is called as Cartesian product. If, WHERE clause is used with CROSS JOIN, it functions like an INNER JOIN. An alternative way of achieving the same result is to use column names separated by commas after SELECT and mentioning the table names involved, after a FROM clause. Syntax: Cross join syntax is as follows: SELECT <column list> FROM <left joined table> CROSS JOIN <right joined table> Example: Select ename,mgr, hiredate, dept.dname, dept.loc from emp CROSS JOIN dept Outer join: Left outer join: The SQL LEFT JOIN, joins two tables and fetches rows based on a condition, which are matching in both the tables, and the unmatched rows will also be available from the table before the JOIN clause. Syntax: Left join syntax is as follows: SELECT <column list> FROM <left joined table> LEFT OUTER JOIN <right joined table> ON <join condition> Example: Select ename,mgr, hiredate, dept.dname, dept.loc from emp LEFT OUTER JOIN dept on emp.deptno = dept.deptno; Right outer join: The SQL RIGHT JOIN, joins two tables and fetches rows based on a condition, which are matching in both the tables, and the unmatched rows will also be available from the table written after the JOIN clause. Syntax: Right join syntax is as follows: SELECT <column list> FROM <left joined table> RIGHT OUTER JOIN <right joined table> ON <join condition>

Example: Select ename,mgr, hiredate, dept.dname, dept.loc from emp RIGHT OUTER JOIN dept on emp.deptno = dept.deptno; Full outer join: The FULL OUTER JOIN will return all rows, as long as there's matching data in one of the tables. It includes all the rows from both the participating tables and does not select either the LEFT or RIGHT table from the JOIN key word. The FULL OUTER JOIN combines the results of both left and right outer joins. When no matching rows exist for rows on the left side of the JOIN key word, NULL values will be returned from the result set on the right. On the other hand , when no matching rows exist for rows on the right side of the JOIN key word, NULL values will be returned from the result set on the left. Syntax: Full outer join syntax is as follows: SELECT <column list> FROM <left joined table> FULL OUTER JOIN <right joined table> ON <join condition> Example: Select ename,mgr, hiredate, dept.dname, dept.loc from emp FULL OUTER JOIN dept on emp.deptno = dept.deptno; Self join: A SELF JOIN is another type of join in sql which is used to join a table to itself, specially when the table has a FOREIGN KEY which references its own PRIMARY KEY. In this join, the participating table appears twice after the FROM clause and is followed by aliases for the tables that qualify column names in the join condition. In this join those rows are returned from the table which are satisfying the conditions. Syntax: Self join syntax is as follows: SELECT e1.ename||' works for '||e2.ename "Employees and their Managers" FROM emp e1, emp e2 WHERE e1.mgr = e2.empno; Example: Select e2.ename from emp e1, emp e2 where e1.mgr = e2.empno;

Вам также может понравиться