83
SQL RIGHT JOIN
The SQL right join returns all the values from the rows of right table. It also includes the matched values from left table but if there is no matching in both tables, it returns NULL.
Basic syntax for right join:
let us take an example with 2 tables table1 is CUSTOMERS table and table2 is ORDERS table.
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 |
and this is the second table:
ORDER TABLE:
DATE | O_ID | CUSTOMER_ID | AMOUNT |
---|---|---|---|
20-01-2012 | 001 | 2 | 3000 |
12-02-2012 | 002 | 2 | 2000 |
22-03-2012 | 003 | 3 | 4000 |
11-04-2012 | 004 | 4 | 5000 |
Here we will join these two tables with SQL RIGHT JOIN:
ID | NAME | AMOUNT | DATE |
---|---|---|---|
2 | AROHI | 3000 | 20-01-2012 |
2 | AROHI | 2000 | 12-02-2012 |
3 | VINEET | 4000 | 22-03-2012 |
4 | AJEET | 5000 | 11-04-2012 |
Next TopicSQL Full Join