SQL99 Syntax Displaying Data from Multiple Tables (SQL99 Syntax with examples) 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 Natural Join Example • 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. 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; 1 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. 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. 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 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); 2 Three-Way Join Example (1) Three-Way Join Example (2) Select employees and their departments and customers that they work with Select employees and their departments and customers that they work with • SQL99 • Oracle 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); 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 (1) Left Outer Join Example (2) Select all employees, their job and customers that they work with and customers addresses Select all employees, their job and customers that they work with and customers addresses • SQL99 • Oracle SELECT a.ename, a.job, b.name, b.address FROM emp a LEFT OUTER JOIN customer b ON (a.empno = b.repid); SELECT a.ename, a.job, b.name, b.address FROM emp a, customer b WHERE a.empno = b.repid(+); 3 Right Outer Join Example (1) Right Outer Join Example (2) Select all departments, their location and employees that work in them Select all departments, their location and employees that work in them • SQL99 • Oracle SELECT a.ename, b.dname, b.loc FROM emp a RIGHT OUTER JOIN dept b ON (a.deptno = b.deptno); SELECT a.ename, b.dname, b.loc FROM emp a, dept b WHERE a.deptno(+) = b.deptno; Additional Conditions Select employees and their department where departments location is DALLAS • SQL99 SELECT a.empno, a.ename, b.dname, b.loc FROM emp a JOIN dept b ON (a.deptno = b.deptno) AND loc = ‘DALLAS’; • Oracle SELECT a.empno, a.ename, b.dname, b.loc FROM emp a, dept b WHERE a.deptno = b.deptno AND loc = ‘DALLAS’; 4