NON EQUIJOINS

advertisement
CARTESIAN JOIN
Results from an invalid or omitted join condition; all combinations of rows are displayed.
The ANSI/ISO SQL: 1999 SQL equivalent of
the Cartesian product is the cross-join.
In the ANSI syntax, you can use the NATURAL JOIN, CROSS JOIN, LEFT JOIN, RIGHT JOIN,
FULL JOIN keywords to specifying the type of join.
Page: 1
EQUIJOIN
If a query is relating two tables using a = operator, it is an equality join, also known as a inner join or
an equijoin. Combines rows from two tables that have equivalent values for the specified columns.
Inner Join or Simple Join – returns only the rows that satisfy the join condition.
NATURAL JOIN = EQUIJOIN
An equijoin returns all rows whose values match in both tables. The ANSI/ISO SQL: 1999 join that
accomplishes the same result is called a natural join. A natural join is based on all columns in the
two tables that have the same name and selects rows from the two tables that have equal values
in all matched columns. When using a natural join, it is possible to join the tables without having to
explicitly specify the columns in the corresponding table. However, the names and data types in
both columns must be the same.
Page: 2
NATURAL JOIN – USING CLAUSE
In a natural join, if the tables have columns with the same names but different data types, the join
causes an error. To avoid this situation, the join clause can be modified with a USING clause. The
USING clause specifies the columns that should be used for the equijoin. The query shown is an
example of the USING clause. The columns referenced in the USING clause should not have a
qualifier (table name or alias) anywhere in the SQL statement.
NATURAL JOIN – ON CLAUSE
The ON clause can be used to specify columns to join. The advantage of the ON clause is the
ability to specify the join conditions separate from the WHERE clause. The WHERE clause can
then be used for search or filter conditions.
The ON clause can also be used to specify arbitrary conditions such as joining columns that have
different names. In the example, the ON clause is used in a self-join where the same table is given
two different references. In the employees table, some employees are also managers. The selfjoin is used to select those employees who are also managers
SELECT e.last_name as "EMP", w.last_name as "MGR"
FROM employees e JOIN employees w
ON (e.manager_id = w.employee_id)
Page: 3
INNER JOIN
A join that selects only matching rows of both tables, this is the default type of join.
Page: 4
NON EQUIJOINS
If any other operator is used to join two table, it is a nonequijoin. > >= < <= <> LIKE, BETWEEN..AND
Page: 5
OUTER JOINS
A join used to select data from tables even if there is no matching row in the joined table. These are
rows that are NOT returned by using a simple join. An outer join is specified by the outer-join operator
(+) or the FULL | LEFT | RIGHT OUTER JOIN keyword.
An outer join is used to see rows that have a corresponding value in another table plus those rows in one
of the tables that have no matching value in the other table. To indicate which table may have missing
data use, a plus sign (+) after the table's column name in the query. The query below uses the plus sign
to indicate the table whose column is missing data. The variations of the outer join are shown.
Page: 6
Page: 7
SELF JOIN
A self-join joins a table to itself. When performing self-joins in ANSI syntax, use the JOIN.. ON
syntax. Do not use NATURAL JOIN and JOIN USING.
In data modeling, it was sometimes necessary to show an entity with a relationship to itself. For
example, an employee can also be a manager. We showed this using the "pig’s ear" relationship.
Once we have a real employees table, a special kind of join called a self-join is required to access
this data. You probably realize by now the importance of a data model once it becomes a
database. It’s no coincidence that the data model looks a lot like the tables we now have in the
database.
JOIN ON – when you do not have common column
names between tables
The ON clause can also be used to specify
arbitrary conditions such as joining columns that
have different names. In the example, the ON
clause is used in a self-join where the same table
is given two different references. In the employees
table, some employees are also managers. The
self-join is used to select those employees who are
also managers.
SELECT worker.last_name || ' works for ' || manager.last_name
FROM employees worker INNER JOIN employees manager
ON worker.employee_id = manager.employee_id
Page: 8
Page: 9
Download