Displaying Data from Multiple Tables (SQL99 Syntax with examples) SQL99 Syntax 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. Cross Join Example Select all employees and departments • SQL99 SELECT ename, dname FROM emp CROSS JOIN dept; • Oracle SELECT ename, dname FROM emp, dept; 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. 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; 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. 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); 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. 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); 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. 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); 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); 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; 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. 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. 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); 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);