I need some help with this SQL Query. It is designed to retrieve names of students with the same S.S_level values as Jaci Walker, and have taken courses (CS.C_SE_id) with Jaci Walker in the BG building.
I am having trouble on line 7. I need to be able to ensure that the people have enrolled in the same course as Jaci Walker. I'm not sure about what to put in the WHERE statement for that section.
The database schema can be seen here:
SELECT S.S_Fname, S.S_LName
FROM Student S, Enrollment E, CourseSection CS, Location L
WHERE S.S_id = E.S_id
AND E.C_SE_ID = CS.C_SE_id
AND L.Loc_id = CS.Loc_ID
AND S.S_Level = (SELECT S.S_Level FROM Student S WHERE S.S_Fname = "Jaci" AND S.S_Lname = "Walker")
AND CS.C_SE_id = (SELECT CS.C_SE_id FROM CourseSection CS WHERE **?**)
AND L.Loc_id = (SELECT L.Blodg_code FROM Location L WHERE L.Blodg_code = "BG");
IF I'm understanding the question right this should work:
SELECT s.S_Fname, s.S_LName
FROM Student s
INNER JOIN Enrollment e ON s.S_id = e.S_id
INNER JOIN CourseSection cs ON e.C_SE_ID = cs.C_SE_id
INNER JOIN Location l ON cs.Loc_ID = l.Loc_id
INNER JOIN Student s2 ON s.S_Level = s2.S_Level AND s2.S_Fname = "Jaci" AND s2.S_Lname = "Walker"
INNER JOIN Enrollment e2 ON s2.S_id = e2.S_id
INNER JOIN CourseSection cs2 ON e2.C_SE_ID = cs2.C_SE_id
WHERE l.Loc_id = l.Blodg_code = "BG"
AND cs.Course_ID = cs2.Course_ID
I'm unable to test at the moment and it was quickly done. Maybe a better solution can be found?
Without access to the data I can't test but the following may help (the first inner select is pulling back all the relevant C_SE_ID's)
SELECT S.S_Fname, S.S_Lname
FROM Student S, Enrollment E
WHERE S.S_id = E.S_id
AND E.C_SE_ID IN (SELECT CS.C_SE_ID
FROM Student S2, Enrollment E2, CourseSection CS, Location L
WHERE S2.S_id = E2.S_id
AND E2.C_SE_ID = CS.C_SE_ID
AND CS.Loc_id = L.Loc_id
AND L.Blodg_code = 'BG'
AND S2.S_Fname = 'Jaci'
AND S2.S_Lname = 'Walker')
AND S.S_Level = (SELECT S3.S_Level
FROM Student S3
WHERE S3.S_Fname = 'Jaci'
AND S3.S_Lname = 'Walker')
AND S.S_Fname <> 'Jaci'
AND S.S_Lname <> 'Walker'
I would recommend using different aliases within inner queries (eg S2, E2) to avoid confusion.
I would start by using current SQL-syntax using JOIN conditions instead of using the WHERE clause to show relationships between tables. This way, you get all your table associations done and can better visually confirm you have those elements configured... THEN, tack on the criteria you are looking for.
What I have done here is to just have a PreQuery (result alias "JaciClassesInBG" ) that gets all of Jaci's classes that were enrolled in and ONLY those for the building "BG" (which was added to the JOIN clause to the location table). The WHERE clause was only for Jaci.
From that result, I have a list of all classes that Jaci took. I grabbed her ID, S_Level and C_SE_ID entries.
From that, just join back to the enrollment table of all other students based explicitly on the C_SE_ID that Jaci took (thus all students in that exact same class). However, I've EXCLUDED (via AND NOT...) Jaci's student ID from the list... we know she took the class, we are looking for everyone ELSE.
Finally, join that result back to the students table based on the common enrollment. Now, we can associate the common "S_LEVEL" criteria of Jaci to those students...
Now, you can get whatever details you want for display... in this case, I am grabbing each student, and what class they had in common with Jaci. One student may have been in multiple classes. This will show each. If you only care about one instance, I would just change the top to...
select DISTINCT S2.S_FName, S2.S_LName...
SELECT
JaciClassesInBG.Course_Code,
JaciClassesInBG.Course_Name,
S2.S_FName,
S2.S_LName
from
( SELECT
S.ID,
S.S_Level,
CS.C_SE_ID,
C.Course_Code,
C.Course_Name
FROM
Student S
JOIN Enrollment E
ON S.S_id = E.S_id
JOIN CourseSection CS
ON E.C_SE_ID = CS.C_SE_id
JOIN Location L
ON L.Loc_id = CS.Loc_ID
AND L.Blodg_Code = "BG"
JOIN Course C
ON CS.Course_ID = C.Course_ID
WHERE
S.S_Fname = "Jaci"
AND S.S_Lname = "Walker" ) JaciClassesInBG
JOIN
Enrollment E2
ON JaciClassesInBG.C_SE_ID = E2.C_SE_ID
AND NOT JaciClassesInBG.S_ID = E2.S_ID
JOIN Students S2
ON E2.S_ID = S2.S_ID
AND JaciClassesInBG.S_Level = S2.S_Level
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.