More Related Content
Similar to Sqlite left outer_joins
Similar to Sqlite left outer_joins (15)
More from Nargis Ehsan (11)
Sqlite left outer_joins
- 1. http://www.tutorialspoint.com/sqlite/sqlite_left_outer_joins.htm Copyright © tutorialspoint.com
SQLITE - LEFT OUTER JOIN
An outer join does not require each record in the two joined tables to have a matching record.
There are three types of outer joins. Left outer joins, right outer joins, and full outer joins. SQLite
only supports left outer joins
The LEFT OUTER JOIN returns all values from the left table, even if there is no match with the
right table. It such rows, there will be NULL values. In other words, left outer join returns all the
values from the left table, plus matched values from the right table. Note, that the OUTER
keyword can be omitted.
Consider following two tables, (a) COMPANY table is as follows:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Paul | 32 | California| 20000.00|
| 2 | Allen | 25 | Texas | 15000.00|
| 3 | Teddy | 23 | Norway | 20000.00|
| 4 | Mark | 25 | Rich-Mond | 65000.00|
| 5 | David | 27 | Texas | 85000.00|
| 6 | Kim | 22 | South-Hall| 45000.00|
| 7 | James | 24 | Houston | 10000.00|
+----+----------+-----+-----------+----------+
(b) Another table is ORDERS as follows:
+-----+---------------------+-------------+--------+
|OID | DATE | CUSTOMER_ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 | 3 | 3000 |
| 100 | 2009-10-08 00:00:00 | 3 | 1500 |
| 101 | 2009-11-20 00:00:00 | 2 | 1560 |
| 103 | 2008-05-20 00:00:00 | 4 | 2060 |
+-----+---------------------+-------------+--------+
Now let us join these two tables in our SELECT statement as follows:
SQL> sqlite> SELECT NAME, AMOUNT ,DATE FROM COMPANY LEFT JOIN ORDER
> ON COMPANY.Id = ORDER.CustomerId;
This would produce following result:
+----+----------+--------+---------------------+
| ID | NAME | AMOUNT | DATE |
+----+----------+--------+---------------------+
| 1 | Paul | 3000 | 2009-10-08 00:00:00 |
| 2 | Allen | 2060 | 2008-05-20 00:00:00 |
| 3 | Teddy | 2060 | 2008-05-20 00:00:00 |
| 4 | Mark | 2060 | 2008-05-20 00:00:00 |
| 5 | David | 2060 | 2008-05-20 00:00:00 |
| 6 | Kim | 2060 | 2008-05-20 00:00:00 |
| 7 | James | 2060 | 2008-05-20 00:00:00 |
+----+----------+--------+---------------------+