Presentation is loading. Please wait.

Presentation is loading. Please wait.

Displaying Data from Multiple Tables (SQL99 Syntax with examples)

Similar presentations


Presentation on theme: "Displaying Data from Multiple Tables (SQL99 Syntax with examples)"— Presentation transcript:

1 Displaying Data from Multiple Tables (SQL99 Syntax with examples)

2 SQL99 Syntax

3 Cross Join The CROSS keyword indicates that a cross join is being performed. A cross join produces the cross-product of two relations and is essentially the same as the comma-delimited Oracle notation.

4 Cross Join Example Select all employees and departments SQL99 SELECT ename, dname FROM emp CROSS JOIN dept; Oracle SELECT ename, dname FROM emp, dept;

5 Natural Join The NATURAL keyword indicates that a natural join is being performed. A natural join 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 the relevant columns. When specifying columns that are involved in the natural join, do not qualify the column name with a table name or table alias. Restriction: You cannot specify a LOB column or a collection column as part of a natural join.

6 Natural Join Example (Equijoin) Select employees, their job and their department, and departments location SQL99 SELECT empno, ename, job, dname, loc FROM emp NATURAL JOIN dept; Oracle SELECT a.empno, a.ename, a.job, b.dname, b.loc FROM emp a, dept b WHERE a.deptno = b.deptno;

7 USING column When you are specifying an equijoin of columns that have the same name in both tables, the USING column clause indicates the columns to be used. You can use this clause only if the join columns in both tables have the same name. Do not qualify the column name with a table name or table alias. Restriction: You cannot specify a LOB column or a collection column in the USING column clause.

8 USING column Example Select employees and their department, and departments location SQL99 SELECT a.empno, a.ename, b.dname, b.loc FROM emp a JOIN dept b USING (deptno);

9 ON condition Use the ON clause to specify a join condition. Doing so lets you specify join conditions separate from any search or filter conditions in the WHERE clause.

10 ON condition Example Select employees and their department, and departments location SQL99 SELECT a.empno, a.ename, b.dname, b.loc FROM emp a JOIN dept b ON (a.deptno = b.deptno);

11 Self Join A self join is a join of a table to itself. This table appears twice in the FROM clause and is followed by table aliases that qualify column names in the join condition. To perform a self join, Oracle combines and returns rows of the table that satisfy the join condition.

12 Self Join Example Select employees and their managers SQL99 SELECT a.ename employee, b.ename manager FROM emp a JOIN emp b ON (a.empno = b.mgr);

13 Three-Way Join Example (1) Select employees and their departments and customers that they work with SQL99 SELECT a.ename employee, b.dname departmant, c.name customer FROM emp a JOIN dept b ON (a.deptno = b.deptno) JOIN customer c ON (a.empno = c.repid);

14 Three-Way Join Example (2) Select employees and their departments and customers that they work with Oracle SELECT a.ename employee, b.dname departmant, c.name customer FROM emp a, dept b, customer c WHERE a.deptno = b.deptno AND a.empno = c.repid;

15 Outer Join (1) An outer join extends the result of a simple join. An outer join returns all rows that satisfy the join condition and also returns some or all of those rows from one table for which no rows from the other satisfy the join condition. –To write a query that performs an outer join of tables A and B and returns all rows from A (a left outer join), use the ANSI LEFT [OUTER] JOIN syntax, or apply the outer join operator (+) to all columns of B in the join condition. For all rows in A that have no matching rows in B, Oracle returns null for any select list expressions containing columns of B.

16 Outer Join (2) –To write a query that performs an outer join of tables A and B and returns all rows from B (a right outer join), use the ANSI RIGHT [OUTER] syntax, or apply the outer join operator (+) to all columns of A in the join condition. For all rows in B that have no matching rows in A, Oracle returns null for any select list expressions containing columns of A. –To write a query that performs an outer join and returns all rows from A and B, extended with nulls if they do not satisfy the join condition (a full outer join), use the ANSI FULL [OUTER] JOIN syntax.

17 Left Outer Join Example Select all employees, their job and customers that they work with and customers addresses SQL99 SELECT a.ename, a.job, b.name, b.address FROM emp a LEFT OUTER JOIN customer b ON (a.empno = b.repid);

18 Right Outer Join Example Select all departments, their location and employees that work in them SQL99 SELECT a.ename, b.dname, b.loc FROM emp a RIGHT OUTER JOIN dept b ON (a.deptno = b.deptno);


Download ppt "Displaying Data from Multiple Tables (SQL99 Syntax with examples)"

Similar presentations


Ads by Google