0 votes
1 view
in SQL by (2.5k points)

I'm trying to join 3 tables in a view; here is the situation:

I have a table that contains information of students who are applying to live on this College Campus. I have another table that lists the Hall Preferences (3 of them) for each Student. But each of these preferences are merely an ID Number, and the ID Number has a corresponding Hall Name in a third table (did not design this database...).

1 Answer

0 votes
by (7.2k points)

Here is what I currently have:

We can simply use Inner-join with 3 tables in SQL.

SELECT ss.collegestudent

    , ss.studentid

    , ss.studentdesc

    , h.hallname

FROM students ss

INNER JOIN hallprefs hp

    on ss.studentid = hp.studentid

INNER JOIN halls h

    on hp.hallid = h.hallid

Based on your request, You just join on your hall table multiple times.

SELECT     ss.StudentID

    , ss.FIRSTName

    , ss.LASTName

    , ss.Gender

    , ss.BirthDate

    , ss.Email

    , r.HallPref1

    , h11.hallName as Pref1HallName

    , r.HallPref2 

    , h22.hallName as Pref2HallName

    , r.HallPref3

    , h33.hallName as Pref3HallName

FROM  dbo.StudentSignUp AS ss 

INNER JOIN RoomSignUp.dbo.Incoming_Applications_Current AS r 

    ON s.StudentID = r.StudentID 

INNER JOIN HallData.dbo.Halls AS h11 

    ON r.HallPref1 = h1.HallID

INNER JOIN HallData.dbo.Halls AS h22

    ON r.HallPref2 = h2.HallID

INNER JOIN HallData.dbo.Halls AS h33

    ON r.HallPref3 = h3.HallID


Related questions

0 votes
1 answer
0 votes
1 answer
asked Jan 7 in SQL by Appu (6.1k points)
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !