SQL Query to join three tables


  • Hello All,

    I want to fetch data from three different table in sql database.

    My scenario is like.

    Table MasterA

    Name                     ID                        Rollno

    ABC                        001                       101

    DEF                        002                       102

    XYZ                        003                       103

    Table MasterB

    NAME                 Class

    ABC                     C1

    ABC                     C2

    DEF                     C3

    DEF                     C2

    Table MasterC

    NAME                      Section

    ABC                          S1

    ABC                           S2

    ABC                          S3

    DEF                           S1

    From three table MasterA , MasterB , MasterC I required the below result


    NAME                    ID                Class                       Section

    ABC                      101               C1                               S1 

    ABC                      101               C2                               S2

    ABC                      101                                                  S3

    DEF                      102                C3                              S1

    DEF                      102                C2                                        


    Tuesday, July 31, 2012 2:36 AM

All replies

  • Hello Prajwal,

    Can you please explain the logic for the result?

    If you simply join all 3 tables by the column "Name" you would get 1 * 2 * 3 = 6 times "ABC" in the result; but you want only 3 times ABC.

    There seems to be a relation between MasterB and MasterC missing.

    Olaf Helper
    * cogito ergo sum * errare humanum est * quote erat demonstrandum *
    Wenn ich denke, ist das ein Fehler und das beweise ich täglich
    Blog Xing

    Tuesday, July 31, 2012 6:13 AM
  • Hello Olaf,

    What ever you understood is correct. I want minimum number of rows containing all information from three table.

    Like for ABC I want only 3 rows, I dont want 6 row.

    There is Name is comman between all three table. There is no relation between MasterB and MasterC.

    If you want any further detail let me know.


    Tuesday, July 31, 2012 6:22 AM