Tuesday, 12 December 2017

SQL Join's Example

'Join' by word itself we get to know, it talks about something we combine together to get desired result.

In database terms, join is all about combining data from multiple tables as single record. Now days, all the data in database is normalized and to display some meaning full information we need to combine data from more than one table.

For example, let's take classic example of customer and order.
In real world, customer information will go in CUSTOMERS table and orders will go in ORDERS table having FOREIGN_KEY for CUSTOMER_ID.
Now when we have to retrieve data for one of use case say, who has placed order with ID=xyz. Here we will get the record from ORDERS, and check for respective customer from CUSTOMERS table using FOREIGN_KEY in that record, combine this data and result will be populated.
Now such operations can be done either by coding or SQL way.

Lets now talk more on SQL Join's in detail -

While talking about SQL Join's, there are 4 basic types of join's as -

  1. Inner
  2. Left
  3. Right
  4. Full
To understand this type quickly let's see below examples with diagrams -

Considering above diagram and join scenario's we might need data like -

  1. Only B              [Inner]
  2. A and B            [Left]
  3. B and C            [Right]
  4. All A, B and C    [Full]
Lets see in detail one by one -

Before going to types, see below database table example, which will be used as reference further to explain all types of Join's.


CUSTOMERS
CUST_IDCUST_NAMECUST_CONTACTCUST_CITY
11111Ram32145646Pune
11112Sarika65465812Mumbai
11113Manisha45454545Delhi
11114Vishal89295956Chennai
11115Mahesh54945956Jaipur


ORDERS
ORDER_IDO_DATEAMOUNTCUST_ID
11122-05-2017120011111
11223-05-2017350011112
11324-05-201720011114
11424-05-2017200011120




Inner Join
    [Select all records from MyTable1 and MyTable2, for which the given join condition is valid]

    Let's say we want to display list of customers whole had made an order, along with the details of order they placed.
    If we consider above diagram, this use case will be, all the records from MyTable1 for which there is reference in MyTable2 (Portion B).
    Best fit example for Inner Join.

    Query:

    SELECT CUST_NAME, CUST_CONTACT, O_DATE, AMOUNT FROM CUSTOMERS C INNER JOIN ORDERS O ON C.CUST_ID = O.CUST_ID;



    INNER JOIN RESULT
    CUST_NAMECUST_CONTACTO_DATEAMOUNT
    Ram3214564622-05-20171200
    Sarika6546581223-05-20173500
    Vishal8929595624-05-2017200



    Left Join

    [Select all records from MyTable1 along with records from MyTable2, for which the given join condition is valid]

    Simply we want to display all the customers and their respective orders.
    i.e. All the records from MyTable1 (A) for which there is reference in MyTable2 (B) and NOT records from MyTable2 (C) for which there is no entry in MyTable1.
    This is good example for Left Join.

    Query:

    SELECT CUST_NAME, CUST_CONTACT, O_DATE, AMOUNT FROM CUSTOMERS C LEFT JOIN ORDERS O ON C.CUST_ID = O.CUST_ID;

    LEFT JOIN RESULT
    CUST_NAMECUST_CONTACTO_DATEAMOUNT
    Ram3214564622-05-20171200
    Sarika6546581223-05-20173500
    Vishal8929595624-05-2017200
    Manisha45454545NULLNULL
    Mahesh54945956NULLNULL



    Right Join

    [Select all records from MyTable2 along with records from MyTable1, for which the given join condition is valid] 
    Now consider that, we have to display all the order details along with customers who had placed the order.

    i.e. All the records from MyTable2 (C), for which there is reference in MyTable1 (B), and NOT there records from MyTable1 (A) which don't have corresponding record in MyTable2.

    Query:


    SELECT CUST_NAME, CUST_CONTACT, O_DATE, AMOUNT FROM CUSTOMERS C RIGHT JOIN ORDERS O ON C.CUST_ID = O.CUST_ID;


    RIGHT JOIN RESULT
    CUST_NAMECUST_CONTACTO_DATEAMOUNT
    Ram3214564622-05-20171200
    Sarika6546581223-05-20173500
    Vishal8929595624-05-2017200
    NULLNULL24-05-20172000




    Full Join


    [Select all records from MyTable1 along with records from MyTable2, regardless of given join condition is valid or not]

    If we have to display all the details of customer tables as well of order tables.

    Query:

    SELECT CUST_NAME, CUST_CONTACT, O_DATE, AMOUNT FROM CUSTOMERS C FULL JOIN ORDERS O ON C.CUST_ID = O.CUST_ID;


    FULL JOIN RESULT
    CUST_NAMECUST_CONTACTO_DATEAMOUNT
    Ram3214564622-05-20171200
    Sarika6546581223-05-20173500
    Vishal8929595624-05-2017200
    NULLNULL24-05-20172000
    Manisha45454545NULLNULL
    Mahesh54945956NULLNULL


    No comments:

    Post a Comment