All for Joomla All for Webmasters

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:

  1. SELECT table1.column1, table2.column2…..
  2. FROM table1
  3. RIGHT JOIN table2
  4. ON table1.column_field = table2.column_field;

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:

  1. SQL> SELECT ID,NAME,AMOUNT,DATE
  2. FROM CUSTOMER
  3. RIGHT JOIN ORDER
  4. ON CUSTOMER.ID = ORDER.CUSTOMER_ID;
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
PinIt
submit to reddit

Leave a Reply

Top