473,385 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Query Help: SELECT Courses where student has all prerequisites

Hi,
I think the subject line pretty much says it all...

Say I have a students-classes database and I add a twist. I want to
filter what courses a student can take by comparing the courses he has
taken to the list of prerequisites for the remaining courses.
Everything is fine if a course only has one or no prerequisite. How
do you express in SQL something like:

"Course C" requires "Course A" and "Course B"

and then leave out all the students who do not have both Course A and
Course B?

What "Course C" requires is easy:

CREATE TABLE RequiresCourse(
Course INT NOT NULL,
Prereq INT NOT NULL,
PRIMARY KEY (Course, Prereq));

SELECT Prereq
FROM RequiresCourse
WHERE Prereq = 'Course C';

but how do I determine who has *all* the different prerequisites for a
course?

No. it's not a homework assignment. Honest. Could be and should be,
but it isn't. I'll read my books anyway... I'm dying to figure this
stupid thing out!

Thanks!
Pieter
Nov 12 '05 #1
1 3985
Peter, it may help to think backwards.

This query produces a list of the prerequisites a student has NOT completed
for a course. If no records are returned, the student is eligible.

SELECT tblCoursePrerequisite.PrerequisiteID
FROM tblCoursePrerequisite LEFT JOIN tblResult
ON tblCoursePrerequisite.PrerequisiteID = tblResult.CourseID
WHERE (tblCoursePrereqisite.ForCourseID = 99) AND NOT EXISTS
(SELECT CourseID FROM tblResult
WHERE (tblResult.StudentID = 999) AND
(tblResult.CourseID = tblCoursePrerequisite.PrerequisiteID) );

Structure assumed:
tblCourse (list of courses):
CourseID as primary key.

tblCoursePrerequisite:
ForCourseID CourseID that has a prerequisite.
PrerequisiteID CourseID that is a prerequisite.
Two one-to-many joins to tblCourse, so a course can have many prerequisites.

tblResult (student results in their courses):
StudentID foreign key to tblStudent.StudentID
CourseID foreign key to tblCourse.CourseID
Result what they achieved
ResultDate when achieved

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Pieter Linden" <pi********@hotmail.com> wrote in message
news:bf**************************@posting.google.c om...
Hi,
I think the subject line pretty much says it all...

Say I have a students-classes database and I add a twist. I want to
filter what courses a student can take by comparing the courses he has
taken to the list of prerequisites for the remaining courses.
Everything is fine if a course only has one or no prerequisite. How
do you express in SQL something like:

"Course C" requires "Course A" and "Course B"

and then leave out all the students who do not have both Course A and
Course B?

What "Course C" requires is easy:

CREATE TABLE RequiresCourse(
Course INT NOT NULL,
Prereq INT NOT NULL,
PRIMARY KEY (Course, Prereq));

SELECT Prereq
FROM RequiresCourse
WHERE Prereq = 'Course C';

but how do I determine who has *all* the different prerequisites for a
course?

No. it's not a homework assignment. Honest. Could be and should be,
but it isn't. I'll read my books anyway... I'm dying to figure this
stupid thing out!

Thanks!
Pieter

Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: alexz | last post by:
If i have an SQL query called "report" which is based on two other queries "student" and "course". I build my queries in Access DB and My student query SELECT tblAuthor.name, courseReg.GradeID,...
2
by: Alan Shiers | last post by:
Hi there, I need help extracting data from three seperate tables. The Tables look like so: Courses Table: COURSE_ID | SUBJECT_TITLE | DESCRIPTION Scheduled_Courses Table: SCHEDULE_ID |...
4
by: Richard Hollenbeck | last post by:
The following query takes about one second to execute with less than 1,000 records, but the report that's based on it takes from 15-30 seconds to format and display. That's frustrating for both me...
15
by: Richard Hollenbeck | last post by:
I tried to ask this question before on the 14th of January but I never got a reply. I'm still struggling with the problem. I'll try to rephrase the question: I have a crosstab query with rows...
6
by: ahin | last post by:
can any one help or give an idea about writing or from where to strat the following program? • Load a list of available courses from a text file into an array (max 4 courses). File format can...
4
by: smsm | last post by:
Project requirements: • Load a list of available courses from the text file “courses.txt” (file is given) into an ArrayList. File format is as follows: 311224 1700 Object Oriented Programming ...
3
by: Richard Hollenbeck | last post by:
I hope this isn't too confusing. The following query runs pretty fast by itself, but when I want to use it in a report (pasted below the query), it takes at least fifteen seconds to run! Then I...
3
by: FNA access | last post by:
Hello all, I am relitivley new to mySQL. I started by downloading from the mySQL site. I then downloaded mySQL administrator and mySQL Query browser. With my set up I got a user root with a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.