MySQL

adplus-dvertising
Join in MySQL
Previous Home Next

Join is use to query the result in the combine form of one or more columns from two or more tables, based on the common fields (like foreign key constraints) defines.

Syntax for join

SELECT columnname1, columnname2
FROM tablename1
JOIN tablename2
ON table1.colname=table2.colname

Example

SELECT *
FROM studadr 
JOIN studinfo 
ON studinfo.sid = studadr.sid ;

The table define after from clause is the first output followed by second table output in single output, here we have define 'studadr' followed by 'studinfo'.

Example

SELECT *
FROM studinfo 
JOIN studadr 
ON studinfo.sid = studadr.sid ;

The above code gives the reverse result as the table order define in the clause.

Example

SELECT fname,age,lname,street
FROM studinfo
JOIN studadr 
ON studinfo.sid = studadr.sid ;

In the above code we can reverse the table name order but the result will remain same, we can change the output order like street , age n anything to get the desired order result.

Example

SELECT fname,age,lname,street
FROM studinfo
JOIN studadr 
ON studinfo.sid = studadr.sid 
WHERE studinfo.sid > 105
order by fname ;

In the above code we have can use either WHERE clause or ORDER BY clause , but here can use both to get the desired output on the specified condition.

So,Join are of four types , they are

  1. Inner Join
  2. Outer Join
  3. Left Join
  4. Right Join
Previous Home Next