Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello community.

I have 4 tables and Im trying to join them. it doesnt work yet. I need to join that user can retrieve the Engineer’s Lastname and Firstname, the Faculty Lastname and Email and the Classes’s Subject and Title for each Course enrollment.

CREATE TABLE Engineers(
EID NUMBER,
Lastname varchar2(50),
Firstname varchar2(50),
Email varchar2(50),
Graddate DATE,
PRIMARY KEY(EID)
);

CREATE TABLE Faculty(
FID NUMBER,
Lastname varchar2(50),
Firstname varchar2(50),
Email varchar2(50),
Hiredate DATE,
PRIMARY KEY(FID)
);


CREATE TABLE Classes(
CID NUMBER,
Subject varchar2(50),
Catalognbr varchar2(50),
Title varchar2(50),
PRIMARY KEY(CID)
);

CREATE TABLE ClassEnrollments(
EnID NUMBER PRIMARY KEY,
EID NUMBER NOT NULL,
FID NUMBER NOT NULL,
CID NUMBER NOT NULL,
CONSTRAINT fk_engineers
FOREIGN KEY (EID)
REFERENCES Engineers(EID) ON DELETE CASCADE,
CONSTRAINT fk_faculty
FOREIGN KEY (FID)
REFERENCES Faculty(FID) ON DELETE CASCADE,
CONSTRAINT fk_classes
FOREIGN KEY (CID)
REFERENCES Classes(CID) ON DELETE CASCADE
);
and that the query that I came up with:

What I have tried:

SELECT
EnID,
Engineer.Lastname AS Lastname,
Engineer.Firstname AS Firstname,

Faculty.Lastname AS Flastname,
Faculty.Email AS email,
Classes.Subject AS Subject,
Classes.Title AS Title,
FROM
ClassEnrollments
INNER JOIN Engineer ON Engineer.EID = ClassEnrollments.EID
INNER JOIN Faculty ON Engineer.FID = ClassEnrollments.FID
INNER JOIN Classes ON Engineer.CID = ClassEnrollments.CID;
Posted
Updated 16-Aug-19 19:15pm
Comments
OriginalGriff 16-Aug-19 10:13am    
And?
What does it do that you didn't expect, or not do that you did?
What did you try to fix it?
Where are you stuck?
What help do you need?
[no name] 16-Aug-19 10:16am    
Join them "two" at a time: (((A + B) + C) + D)
Sergey2121 16-Aug-19 10:17am    
I tried to do JOIN however it doesnt work at all, I think I might be doing something wrong. Could it be that I use wrong join schema or something? I was following tutorials but cant comprehend multiple(more then 2) table joins.
[no name] 16-Aug-19 10:23am    
Just JOIN ... at the end you will only have "engineers with records in all four tables.

INNER and OUTER will creat NULLS here and there when records are "missing".

When you get a problem like this, start by eliminating possibilities.
Comment out most of the query, and see what you get:
SQL
SELECT
EnID
--,Engineer.Lastname AS Lastname
--,Engineer.Firstname AS Firstname
--,Faculty.Lastname AS Flastname
--,Faculty.Email AS email
--,Classes.Subject AS Subject
--,Classes.Title AS Title
FROM
ClassEnrollments
--INNER JOIN Engineer ON Engineer.EID = ClassEnrollments.EID
--INNER JOIN Faculty ON Engineer.FID = ClassEnrollments.FID
--INNER JOIN Classes ON Engineer.CID = ClassEnrollments.CID;
If that produces records, add a bit back:
SQL
SELECT
EnID
,Engineer.Lastname AS Lastname
,Engineer.Firstname AS Firstname
--,Faculty.Lastname AS Flastname
--,Faculty.Email AS email
--,Classes.Subject AS Subject
--,Classes.Title AS Title
FROM
ClassEnrollments
INNER JOIN Engineer ON Engineer.EID = ClassEnrollments.EID
--INNER JOIN Faculty ON Engineer.FID = ClassEnrollments.FID
--INNER JOIN Classes ON Engineer.CID = ClassEnrollments.CID;
And see what that produces. If it all looks good, add some more:
SQL
SELECT
EnID
,Engineer.Lastname AS Lastname
,Engineer.Firstname AS Firstname
,Faculty.Lastname AS Flastname
,Faculty.Email AS email
--,Classes.Subject AS Subject
--,Classes.Title AS Title
FROM
ClassEnrollments
INNER JOIN Engineer ON Engineer.EID = ClassEnrollments.EID
INNER JOIN Faculty ON Engineer.FID = ClassEnrollments.FID
--INNER JOIN Classes ON Engineer.CID = ClassEnrollments.CID;

And so on. When it fails, look closely at the data you just added and see if there is anything obvious.
 
Share this answer
 
I think the problems is with your join conditions
you are again and again referencing engineer.column with your classenrollment.column

try below query:

SELECT
EnID,
Engineer.Lastname AS Lastname,
Engineer.Firstname AS Firstname,

Faculty.Lastname AS Flastname,
Faculty.Email AS email,
Classes.Subject AS Subject,
Classes.Title AS Title,
FROM
ClassEnrollments
INNER JOIN Engineer ON Engineer.EID = ClassEnrollments.EID
INNER JOIN Faculty ON Faculty.FID = ClassEnrollments.FID
INNER JOIN Classes ON Classes.CID = ClassEnrollments.CID;
 
Share this answer
 
Confirm there is a record for each engineer in EVERY table.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900