Home > Back-end >  Using inner join as ALIASES with multiple tables
Using inner join as ALIASES with multiple tables

Time:06-01

I have these tables below:

create table student(
    studentName      varchar (40) not null,
    studentRollNo    varchar (30) primary key, -- also acts as username
    studentPassword  varchar(30) NOT NULL,
    studentGender    varchar(7) default NULL
);
create table supervisors(
    supervisorID varchar(30) foreign key references Faculty(facultyID) unique,
    sWorkLoad int default null,
    CHECK (sWorkLoad<=6 and sWorkLoad>=0)
);
create table co_supervisors(
    co_supervisorID varchar(30) foreign key references Faculty(facultyID) unique,
    csWorkLoad int default null,
    CHECK (csWorkLoad<=6 and csWorkLoad>=0)
);
create table studentGroup(
    groupID int IDENTITY(1,1) primary key ,
    Member1rollNo varchar(30) foreign key references student(studentRollNo) default NULL, -- member 1
    Member2rollNo varchar(30) foreign key references student(studentRollNo) default NULL, -- member 2
    Member3rollNo varchar(30) foreign key references student(studentRollNo) default NULL, -- member 3
    supervID  varchar(30) foreign key references supervisors(supervisorID),
    co_supervID  varchar(30) foreign key references co_supervisors(co_supervisorID) default NULL,
    projectTitle varchar(100) not null,
    projectDetails varchar (500) default NULL
);
create table FYP1(
     groupID int foreign key references studentGroup(groupID),
);

I want to display student details who are registered in FYP1. with their supervisors, co_supervisors,and project title.

But I I'm not able to do so, What I have done so far is this.

select sg.Member1rollNo,S.studentName,Member2rollNo,S.studentName,sg.Member3rollNo,sg.supervID,sg.projectTitle
FROM student S
inner join studentGroup SG ON S.studentRollNo = SG.Member1rollNo

OR some random tries like this

--  Faculty.facultyName
SELECT FYP1.groupID, studentGroup.Member1rollNo,student.studentName as student1, studentGroup.Member2rollNo,student.studentName  as student2,studentGroup.projectTitle
FROM FYP1 as FYP1_Students
INNER JOIN studentGroup ON (studentGroup.groupID = FYP1_Students.groupID)
INNER JOIN supervisors  ON (studentGroup.supervID = supervisors.supervisorID)
INNER JOIN student      ON (student1.studentRollNo = studentGroup.Member1rollNo)
INNER JOIN student      ON (student.studentRollNo = studentGroup.Member2rollNo)

Output or first query is this (example):

'i19-0434'  'Sourav Malani' 'i19-0498'  'Sourav Malani' NULL    'urooj.ghani'   'Indoor Navigation'
'i19-0466'  'Aftab Ali' 'i19-0528'  'Aftab Ali' NULL    'urooj.ghani'   'AI based Physics exp.'

I want output to be like:

'i19-0434'  'Sourav Malani' 'i19-0498'  'Student2 Name' NULL    'urooj.ghani'   'Indoor Navigation'
'i19-0466'  'Aftab Ali' 'i19-0528'  '<student2Name>'    NULL    'urooj.ghani'   'AI based Physics exp.'

Sample Data

Thanks to @Hana, I solved the problem. Here's the solution:

SELECT SG.groupID,
       SG.Member1rollNo,S1.studentName as 'student1  Name',
       SG.Member2rollNo, S2.studentName as 'student2 Name',
       SG.Member3rollNo, S3.studentName as 'Studen3  Name',
       SG.supervID, SN.facultyName as 'Supervisor Name',
       SG.co_supervID, CSN.facultyName as 'Co_Supervisor',
       SG.projectTitle as 'Project Title',
       SG.projectDetails as 'Project Desc.'
FROM FYP1 FYP1
    LEFT OUTER JOIN studentGroup SG ON FYP1.groupID = SG.groupID 
    LEFT OUTER JOIN supervisors SV ON SG.supervID  = SV.supervisorID
    LEFT OUTER JOIN Faculty SN ON   SG.supervID= SN.facultyID
    LEFT OUTER JOIN Faculty CSN ON   SG.co_supervID= CSN.facultyID
    LEFT OUTER JOIN student S1 ON SG.Member1rollNo  = S1.studentRollNo
    LEFT OUTER JOIN student S2 ON SG.Member2rollNo  = S2.studentRollNo
    LEFT OUTER JOIN student S3 ON SG.Member3rollNo  = S3.studentRollNo

CodePudding user response:

Is this what you are looking for?

SELECT SG.groupID, SV.supervisorID, CSV.co_supervisorID, S1.studentName, S2.studentName, S3.studentName  
FROM FYP1 FYP1
    INNER JOIN studentGroup SG ON FYP1.groupID = SG.groupID 
    INNER JOIN supervisors SV ON SG.supervID  = SV.supervisorID
    INNER JOIN co_supervisors CSV ON SG.co_supervID = CSV.co_supervisorID
    INNER JOIN student S1 ON SG.Member1rollNo  = S1.studentRollNo
    INNER JOIN student S2 ON SG.Member1rollNo  = S2.studentRollNo
    INNER JOIN student S3 ON SG.Member1rollNo  = S3.studentRollNo
  • Related