WHY AND HOW TO USE SQL DISTINCT CLAUSE?

Abhishek Srivasatava

Total Post:70

Points:350
Posted by  Abhishek Srivasatava
 354  View(s)
Ratings:
Rate this:
  1. Abhishek Srivasatava

    Post:70

    Points:350
    Why and how to use SQL DISTINCT Clause?

    Distinct is used when unique result is needed from user side. It is used because after joining many tables there is chance of multiple rows for single data may come.

    For example

    Prsn table holding the Adhar number of person.

    And we join this prsn table with prsn_buy_prod table. Let person buy 3 product so three rows will exist.

    Program without distinct:

    SELECT A.PRSN_ADHAR_NUM
    FROM ADHAR A
    LEFT JOIN PRSN_BUY_PROD B
    ON A.PRSN_ADHAR_NUMNUM=B.PRSN_ADHAR_NUM_NUM
    Where A.name= P Raturi
    Output :
    987456321
    987456321
    987456321
    For the above program 3
    rows will come for the single name P Raturi.

    But if use distinct along with select statement in the above program then single record will come. 

    SELECT DISTICT A.PRSN_ADHAR_NUM
    FROM ADHAR A
    LEFT JOIN PRSN_BUY_PROD B
    ON A.PRSN_ADHAR_NUMNUM=B.PRSN_ADHAR_NUM_NUM
    Where A.name= P Raturi
    Output :
    987456321

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!