All for Joomla All for Webmasters

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:

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

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:

  1. SQL SELECT ID, NAME, AMOUNT,DATE
  2. FROM CUSTOMER
  3. LEFT JOIN ORDER
  4. ON CUSTOMER.ID = ORDER.CUSTOMER_ID;

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
PinIt
submit to reddit

Leave a Reply

Top