Search code examples
sqlsybase

need to form SQL join/subQuery on multiple tables


There are 3 tables -

tables --- columns

ABC - abc_id, abc_name, active_flag

XYZ - xyz_id, xyz_name, active_flag

ABC_XYZ - abc_xyz_id, abc_id, xyz_id, active_flag

id column in each table is PK I need to form a query which will return output like - for each abc_name there will be multiple xyz_names

like below -

abc_name1 -

    xyz_name1
    xyz_name2
    xyz_name3
    xyz_name4

abc_name2 -

    xyz_name5
    xyz_name6
    xyz_name2
    xyz_name4

I am using sybase DB in my project

any help will be appreciated


Solution

  • To join all 3 your tables, you can start fro the table ABC_XYZ as shown below-

    You can get some more details HERE about SQL JOIN

    SELECT * 
    -- SELEC * will return all records from all tables
    -- You can also select specific column from different table usinf table Alias and column name
    FROM ABC_XYZ A
    INNER JOIN ABC B ON A.abc_id = B.abc_id 
    INNER JOIN XYZ C ON A.xyz_id = C.xyz_id