105
SQL LEFT JOIN
The SQL left join returns all the values from the left table and it also includes matching values from right table, if there are no matching join value it returns NULL.
BASIC SYNTAX FOR LEFT JOIN:
let us take two tables in this example to elaborate all the things:
CUSTOMER TABLE:
ID | NAME | AGE | SALARY |
---|---|---|---|
1 | ARYAN | 51 | 56000 |
2 | AROHI | 21 | 25000 |
3 | VINEET | 24 | 31000 |
4 | AJEET | 23 | 32000 |
5 | RAVI | 23 | 42000 |
This is second table
ORDER TABLE:
O_ID | DATE | CUSTOMER_ID | AMOUNT |
---|---|---|---|
001 | 20-01-2012 | 2 | 3000 |
002 | 12-02-2012 | 2 | 2000 |
003 | 22-03-2012 | 3 | 4000 |
004 | 11-04-2012 | 4 | 5000 |
join these two tables with LEFT JOIN:
This will produce the following result:
ID | NAME | AMOUNT | DATE |
---|---|---|---|
1 | ARYAN | NULL | NULL |
2 | AROHI | 3000 | 20-01-2012 |
2 | AROHI | 2000 | 12-02-2012 |
3 | VINEET | 4000 | 22-03-2012 |
4 | AJEET | 5000 | 11-04-2012 |
5 | RAVI | NULL | NULL |
Next TopicSQL Right Join