zmień kontrast font size: A A A
rss polski
you are at Encyclopedia of SQL >> OUTER JOIN

OUTER JOIN

OUTER JOIN returns all records of one table if even not every record is matched with record in second table. There are few kinds of JOINS :
LEFT JOIN - returns all rows of left table even there don't matched with records in second table
RIGHT JOIN - returns all records of right table even they aren't matched with records in left table
In some databases above joins are known accordingly as LEFT OUTER JOIN and RIGHT OUTER JOIN

Syntax


SELECT `column_name1`,`column_name2`
FROM `table_name1`
LEFT or RIGHT JOIN `column_name2`
ON `table_name1`.`column_name1`=`table_name2`.`column_name2`

Example of LEFT JOIN

Display name and surname of people from `people` table with order amount from `orders` table.

idnamesurnameage
1PawełKowalski3
2PiotrJanik7
3MichałNowak13

idorder_amountperson_id
11001
22002
31002
410020

query


SELECT *
FROM `people`
LEFT JOIN `orders`
ON `orders`.`person_id`=`people`.`id`

result

idnamesurnameageidorder_amountperson_id
1PawełKowalski311001
2PiotrJanik722002
2PiotrJanik731002
3MichałNowak13NULLNULLNULL

At Oracle there is used plus (+) after WHERE keyword on the second side of table name, which we want to display all records. Code from this example is as the follows:

    
    SELECT *
    FROM `people`,`orders`
    WHERE `people`.`id`=`orders`.`person_id`(+)
    
    

Example of RIGHT JOIN

Display all orders with name and surname of customer from `people` table

idnamesurnameage
1PawełKowalski3
2PiotrJanik7
3MichałNowak13

idorder_amountperson_id
11001
22002
31002
410020

query


SELECT *
FROM `people`
RIGHT JOIN `orders`
ON `orders`.`person_id`=`people`.`id`

result

idorder_amountperson_ididnamesurnameage
110011PawełKowalski3
220022PiotrJanik7
310022PiotrJanik7
410020NULLNULLNULLNULL

[ wróć na górę strony ]