SQL JOIN OPERATIONS


The relational join operation merges rows from two tables and returns the rows with one of the following conditions:
·         Have common values in common columns(natural join)
·         Meet a given join condition(equality or inequality)
·         Have a common value in common columns or have no matching values(outer join)

SQL JOIN EXPRESSIN STYLES

JOIN CLASSIFICATION
JOIN TYPE
SQL SYNTAX EXAMPLE
DESCRIPTION
CROSS
CROSS JOIN
SELECT *
FROM T1,T2;    (old style)

SELECT *
FROM T1 CROSS JOIN T2;
Returns the Cartesian product of T1 and T2.
INNER
Old-style join
SELECT *
FROM T1,T2
WHERE T1.C1=T2.C1;
Returns only the rows that meet the join condition in the where clause (old style).Only the rows with matching values are selected.

NATURAL JOIN
SELECT *
FROM T1 NATURAL JOIN T2;
Returns only the rows with matching values in the matching columns. The matching columns must have the same names and similar data types.

JOIN USING
SELECT *
FROM T1 JOIN T2 USING( C1)
Returns only the rows with matching values in the columns indicated in the USING clause

JOIN ON
SELECT *
FROM T1 JOIN T2
            ON T1.C1=T2.C1;
Returns only the rows with matching values in the columns indicated in the ON clause
OUTER
LEFT JOIN
SELECT *
FROM T1 LEFT OUTER JOIN T2 ON T1.C1=T2.C1;
Returns rows with matching values and includes all rows from the left table (T1) with unmatched values.

RIGHT JOIN
SELECT *
FROM T1 RIGHT OUTER JOIN T2     ON T1.C1=T2.C1;
Returns rows with matching values and includes all rows from the right table (T2) with unmatched values.

FULL JOIN
SELECT *
FROM T1 FULL OUTER JOIN T2   ON T1.C1=T2.C1;
Returns rows with matching values and includes all rows from both tables (T1and T2) with unmatched values.
Consider the tables CUSTOMER and AGENT; perform different SQL join operations and write corresponding results
CUSTOMER                                                                                       AGENT
AGENT_CODE
AGENT_PHONE
125
9985707288
167
8985291308
231
9885434311
333
9704128379
CUS_CODE
CUS_NAME
CUS_ZIP
AGENT_CODE
1132445
W
145
231
1217782
A
145
125
1312243
Ra
129
167
1321242
Ro
134
125
1542311
S
134
421
1657399
V
145
231









CROSS JOIN:
SQL> select *                                                 (old style)
  2      from customer,agent;
  CUS_CODE CUS_NAME      CUS_ZIP   AGENT_CODE AGENT_CODE AGENT_PHONE
  -------------- ----------------- ---------------  ----------------- -------------------- ---------------------
   1132445       w                                 145           231                   125                        9985707288
   1217782       a                                  145           125                   125                        9985707288
   1312243       ra                                 129           167                   125                        9985707288
   1321242       ro                                 134           125                   125                        9985707288
   1542311       s                                   134           421                   125                        9985707288
   1657399       v                                  145           231                   125                        9985707288
   1132445       w                                 145           231                   167                        8985291308
   1217782       a                                  145           125                   167                        8985291308
   1312243       ra                                 129           167                   167                        8985291308
   1321242       ro                                 134           125                   167                        8985291308
   1542311       s                                   134           421                   167                        8985291308

  CUS_CODE   CUS_NAME    CUS_ZIP   AGENT_CODE   AGENT_CODE   AGENT_PHONE
  -------------- ----------------- ---------------  -----------------   ------------------- ---------------------
   1657399       v                                  145                   231                 167                  8985291308
   1132445       w                                 145                   231                 231                  9885434311
   1217782       a                                  145                   125                 231                  9885434311
   1312243       ra                                 129                   167                 231                  9885434311
   1321242       ro                                 134                   125                 231                  9885434311
   1542311       s                                   134                   421                 231                  9885434311
   1657399       v                                  145                   231                 231                  9885434311
   1132445       w                                 145                   231                 333                  9704128379
   1217782       a                                  145                   125                 333                  9704128379
   1312243       ra                                 129                   167                 333                  9704128379
   1321242       ro                                 134                   125                 333                  9704128379

   CUS_CODE   CUS_NAME   CUS_ZIP   AGENT_CODE   AGENT_CODE   AGENT_PHONE
    ------------  -----------------   -------------  -------------------  -------------------   ----------------------
     1542311     s                                   134                  421                  333                  9704128379
     1657399                 v                                  145                  231                  333                  9704128379

24 rows selected.
                   SQL> select *
                   2        from customer cross join agent;
This query also results the values same as the above query which is the old style



Old-style join :
SQL> select *
  2     from customer,agent
  3     where customer.agent_code=agent.agent_code;

  CUS_CODE CUS_NAME   CUS_ZIP   AGENT_CODE   AGENT_CODE   AGENT_PHONE
  --------------  ---------------   -----------  -------------------    ------------------     -------------------
       1132445   w                         145                          231                  231                  9885434311
       1217782   a                          145                          125                  125                  9985707288
       1312243   ra                         129                          167                  167                  8985291308
       1321242   ro                         134                          125                  125                  9985707288
       1657399   v                          145                          231                  231                  9885434311


SQL> select *
  2     from customer natural join agent;

AGENT_CODE   CUS_CODE   CUS_NAME   CUS_ZIP     AGENT_PHONE
  -----------------  ---------------   ---------------    ------------    --------------------    
                   231           1132445          w                         145              9885434311
                   125           1217782          a                          145              9985707288
                   167           1312243          ra                         129              8985291308
                   125           1321242          ro                         134              9985707288
                   231           1657399          v                          145              9885434311

  


SQL> select *
  2     from customer join agent using(agent_code);

AGENT_CODE   CUS_CODE   CUS_NAME   CUS_ZIP     AGENT_PHONE
  -----------------  ---------------   ---------------    ------------    --------------------    
                   231           1132445          w                         145              9885434311
                   125           1217782          a                          145              9985707288
                   167           1312243          ra                         129              8985291308
                   125           1321242          ro                         134              9985707288
                   231           1657399          v                          145              9885434311


SQL> select *
  2     from customer join agent on customer.agent_code=agent.agent_code;


CUS_CODE CUS_NAME   CUS_ZIP   AGENT_CODE   AGENT_CODE   AGENT_PHONE
  --------------  ---------------   -----------  -------------------    ------------------     -------------------
       1132445   w                         145                          231                  231                  9885434311
       1217782   a                          145                          125                  125                  9985707288
       1312243   ra                         129                          167                  167                  8985291308
       1321242   ro                         134                          125                  125                  9985707288
       1657399   v                          145                          231                  231                  9885434311


  OUTER JOIN


LEFT OUTER JOIN:
SQL> select *
  2     from customer left outer join agent on customer.agent_code=agent.agent_code;

  CUS_CODE   CUS_NAME      CUS_ZIP   AGENT_CODE   AGENT_CODE AGENT_PHONE
----------------   ------------------- ------------  ------------------   -------------------   ---------------------
      1321242      ro                               134                  125                              125                  9985707288
      1217782      a                                145                  125                              125                  9985707288
      1312243      ra                               129                  167                              167                  8985291308
      1657399      v                                145                  231                              231                  9885434311
      1132445      w                               145                  231                              231                  9885434311
      1542311      s                                 134                  421

6 rows selected.

SQL> select *
  2     from customer right outer join agent on customer.agent_code=agent.agent_code;

  CUS_CODE   CUS_NAME     CUS_ZIP   AGENT_CODE   AGENT_CODE     AGENT_PHONE
 ----------------  ------------------- ------------  ------------------   -------------------    -----------------------
      1132445      w                               145                  231                              231                  9885434311
      1217782     a                                 145                  125                              125                  9985707288
      1312243      ra                               129                  167                              167                  8985291308
      1321242      ro                               134                  125                              125                  9985707288
      1657399      v                                145                  231                              231                  9885434311
                                                                                                                        333                  9704128379

6 rows selected.




SQL> select *
  2     from customer full outer join agent on customer.agent_code=agent.agent_code;

  CUS_CODE   CUS_NAME     CUS_ZIP   AGENT_CODE   AGENT_CODE     AGENT_PHONE
 ----------------  ------------------- ------------  ------------------   -------------------    ----------------------- 
    1321242                    ro                               134                   125                             125                  9985707288
    1217782                    a                                145                   125                             125                  9985707288
    1312243                    ra                               129                   167                             167                  8985291308
    1657399       v                                145                   231                             231                  9885434311
    1132445       w                               145                   231                             231                  9885434311
    1542311                    s                                 134                   421
                                                                                                                        333                  9704128379

7 rows selected.


Note:
We can use USING clause for the outer joins instead of ON.
If ON condition is used the output includes the same columns in two tables twice in the result
If USING clause is used the column appears once in the result