Q:

How to fetch data from tables where there is one to many relationship?

I have created three tables like Product, Brands and ProductBrands in ms sql server
These tables contains data as the following
Products (PId - PName)

1 - Mobiles
2 - T.V
3 - A.C

Brands (BId - BName)

1 - Samsung
2 - Sony
3 - L.G

ProductsBrands (PBId - PId - BId)

1 - 1 - 1
2 - 1 - 2
3 - 1 - 3
4 - 2 - 1
5 - 2 - 3

...
...
Now I have to fetch from tables the following feilds

Product Id
PName 
BId
BName

Basically I have to fetch all the Brands assigned to a Products
Please help me!!! I am very new to this

A:

SELECT  P.PId ,
        P.PName,
        B.BId,
        B.BName
FROM    Products P
JOIN    ProductsBrands  PB On PB.PId  = P.PId 
JOIN    Brands  B ON B.BId  = PB.BId 

See, whether this is what you are looking for.

