|
|
OUTER JOINs
A table resulting from an inner join, as just described, will only contain those rows that satisfy the applicable join condition. This means that a row in either table which does not match a row in the other table will be excluded from the result.
In an
OUTER JOIN, however, a row that does not match a row in the other table is also included in the result table. Such a row appears once in the result and the columns that would normally contain information from the other table will contain theNULLvalue.LEFT OUTER JOIN
The join variants (
JOIN ON,NATURAL JOINandJOIN USING) can be applied as aLEFT OUTER JOIN. In addition to theINNER JOINresult, theLEFT OUTER JOINalso includes the rows fromtable-reference-1(the table on the left of theJOIN) which do not satisfy the join condition.Syntax
The syntax variants of the
LEFT OUTER JOINare as follows:Example
The query below will return a result set containing all employees at least once even though they might not have an entry in the
SALARIEStable.SELECT * FROM employees LEFT OUTER JOIN salaries ON employees.id = salaries.idRIGHT OUTER JOIN
The join variants (
JOIN ON,NATURAL JOINandJOIN USING) can be applied as aRIGHT OUTER JOIN.In addition to the
INNER JOINresult, theRIGHT OUTER JOINalso includes the rows fromtable-reference-2(the table on the right of theJOIN) which do not satisfy the join condition.Syntax
The syntax for the variants of the
RIGHT OUTER JOINis as follows:Example
The query below is roughly similar to the example in the previous section, but the salaries come to the left in the column list.
SELECT * FROM salaries RIGHT OUTER JOIN employees ON salaries.emp_no = employees.emp_noFULL OUTER JOIN
A
FULL OUTER JOINcombines the effect of both aLEFT OUTER JOINand aRIGHT OUTER JOIN, i.e. theFULL OUTER JOINincludes the rows from the left table which do not satisfy the join condition and the rows from the right table which do not satisfy the join condition.Mimer SQL does not support the
FULL OUTER JOINsyntax, but a full outer join can be achieved by performingUNION ALLbetween anINNER JOINand the "extra" rows from the left and right tables respectively.Example
A full outer join of the
SELLERStable with theSUPPLIERStable on theCITYcolumn:SELECT sellers.seller_name, suppliers.supplier_name FROM sellers FULL OUTER JOIN suppliers ON sellers.city = suppliers.citySELECT sellers.seller_name, suppliers.supplier_name FROM sellers JOIN suppliers ON sellers.city = suppliers.city UNION ALL SELECT sellers.seller_name, NULL FROM sellers WHERE NOT EXISTS (SELECT * FROM suppliers WHERE sellers.city = suppliers.city) UNION ALL SELECT NULL, suppliers.supplier_name FROM suppliers WHERE NOT EXISTS (SELECT * FROM sellers WHERE sellers.city = suppliers.city)
|
Mimer Information Technology AB Voice: +46 18 780 92 00 Fax: +46 18 780 92 40 info@mimer.se |
|
|