by Dinesh Thakur

Join is a keyword in SQL that is used to retrieve data from two or more table based on some relationship between two columns.

Type of Joins

EQUI join: It returns all the matched records from the table based on the condition.

Query: Select empno, ename, job, dname, loc from emp1,deptl where  emp1.deptno=deptl.deptno

Left outer join: It returns all the records from left-side table and only matched records from right-side based on the joining condition.

Query: Select empno, ename, job, dname, loc from emp1,deptl where emp1.deptno=dept1.deptno(+)

Right outer join: It returns all the records from right-side table and only matched records from left-side table based on the joining condition.

Query: Select empno, ename, job, dname, loc from emp1,deptl where  emp1.deptno(+)=dept1.deptno

Full join: It returns all the records from the two or more tables based on joining condition.

Query: Select empno, ename, job, dname, loc from emp1full outer join deptl on (emp1.deptno=dept1.deptno)

              JDBC Join

              JDBC Join

Example of showing EQUI join

Source code for the program:

join.java

import java.sql.*;

public class join

{

       public static void main (String arg [])

         {

                try

                 {

                      Class.forName ("oracle.jdbc.driver.oracleDriver");

                      Connection con = DriverManager.getConnection ("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger");

                      Statement st = con.createStatement ();

                      String query = "select empno, ename, job, dname, loc from emp1, dept1 where emp1.deptno=dept1.deptno";

                      ResultSet rs = st.executeQuery (query);

                      System.out.println ("empno\t ename \t job dname \t \t loc");

                      while (rs.next ())

                        {

                              int empno = rs.getInt (1);

                              String ename = rs.getString (2);

                              String job = rs.getString (3);

                              String dname = rs.getString (4);

                              String loc = rs.getString (5);

                              System.out.println (empno+ " "+ename+ " "+job+ " "+dname+ " "+ loc);

                        }

                        st.close ();

                        con.close ();

                 }

                catch (Exception ee)          

                 {

                      System.out.println(ee);

                 }

        }

}

 

                    JDBC Join

Based on joining condition all records have been displayed here.

 

Example on Left outer join

 

Outer.java

import java.sql.*;

public class outer

{

        //code same as the above application

        ……………………………………..

        ……………………………………..

        only change this line of code

        String query = “select empno, ename, job, dname, loc from emp1, dept1 where

        emp1.deptno=dept1.deptno (+)”;

        ……………………………………..

}

 

                     JDBC Join

 Right outer join

                     JDBC Join

 

 Full join

 

                JDBC Join