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

Displaying Data from Multiple Tables (SQL99 Syntax with examples)

Embed Size (px)

Citation preview

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

Displaying Datafrom Multiple Tables

(SQL99 Syntax with examples)

Page 2: Displaying Data from Multiple Tables (SQL99 Syntax with examples)

SQL99 Syntax

Page 3: 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.

Page 4: Displaying Data from Multiple Tables (SQL99 Syntax with examples)

Cross Join Example

Select all employees and departments

• SQL99

SELECT ename, dname

FROM emp CROSS JOIN dept;

• Oracle

SELECT ename, dname

FROM emp, dept;

Page 5: Displaying Data from Multiple Tables (SQL99 Syntax with examples)

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.

Page 6: Displaying Data from Multiple Tables (SQL99 Syntax with examples)

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;

Page 7: Displaying Data from Multiple Tables (SQL99 Syntax with examples)

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.

Page 8: Displaying Data from Multiple Tables (SQL99 Syntax with examples)

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);

Page 9: Displaying Data from Multiple Tables (SQL99 Syntax with examples)

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.

Page 10: Displaying Data from Multiple Tables (SQL99 Syntax with examples)

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);

Page 11: Displaying Data from Multiple Tables (SQL99 Syntax with examples)

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.

Page 12: Displaying Data from Multiple Tables (SQL99 Syntax with examples)

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);

Page 13: Displaying Data from Multiple Tables (SQL99 Syntax with examples)

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);

Page 14: Displaying Data from Multiple Tables (SQL99 Syntax with examples)

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;

Page 15: Displaying Data from Multiple Tables (SQL99 Syntax with examples)

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.

Page 16: Displaying Data from Multiple Tables (SQL99 Syntax with examples)

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.

Page 17: Displaying Data from Multiple Tables (SQL99 Syntax with examples)

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);

Page 18: Displaying Data from Multiple Tables (SQL99 Syntax with examples)

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);