SQL: Access query unique meets multiple criterias - sql

I have a table containing the columns studentID, course and start_date where studentID is unique.
I want to write a query so that students that haven't gone to any courses with a start date within the year 2010 appears. My problem is that I seem to return all students that have gone to any course that isn't 2010 even though they have attended a course during 2010.
SELECT DISTINCT studentID
FROM attendant
WHERE Startdatum NOT BETWEEN #1/1/2010# AND #12/31/2010#
What I'm really asking is; Is there any way to not show a row if it contains a certain value? How?
Subquery of some sort?

It should be something like:
SELECT DISTINCT studentID FROM attendant
WHERE studentID NOT IN
(SELECT DISTINCT studentID FROM attendant
WHERE Startdatum BETWEEN #1/1/2010# AND #12/31/2010#)

A good way to answer this question is using the having clause:
SELECT studentID
FROM attendant
GROUP BY studentID
HAVING sum(iif(Startdatum BETWEEN #1/1/2010# AND #12/31/2010#, 1, 0)) = 0;
The having clause is counting the number of courses that each student took during the time frame. If there are any, then the filter fails.

Related

SQL - Not sure if I should use joins to resolve this query

I have the following question presented to me:
"Write a SQL command to display for each student who has an average result of greater than 60, the student’s name, the total number of attempts the student has had at all exams, and the student’s average result."
The tables I am working with for this are:
Mark, with fields examID(PK), studentID(PK), result, occurred, noOfAttempts
And Student with the fields id(PK) and Name.
The field StudentID in mark is also a foreign key to Student.
Anyways this is what I've come up with so far:
SELECT S.name, AVG(M.RESULT) FROM STUDENT S, MARK M
WHERE S.ID = (
SELECT studentID FROM
MARK
WHERE 60 <
(SELECT AVG(result) from MARK));
But it is igiving me a "not a single group function" error. I've tried using some other joins but that also resulted in a whole ton of errors so I'm pretty sure I did those wrong.
At this point I am nearly completely lost. I know that the "deepest" subquery will return me the student ID's that have an average result bigger than 60, but when I try to return all student ID's that match that particular ID it doesnt work. I'm also not sure how to count the total no. of attempts in this query so I didnt even attempt that for now.
Table data:
You can try something like that:
SELECT
S.name, SUM(M.noOfAttempts) AS attempts, AVG(M.RESULT) AS result
FROM
STUDENT S
INNER JOIN
MARK M ON (S.ID = M.studentID)
GROUP BY
S.ID, S.name
HAVING
AVG(M.RESULT) > 60

SQL query to return only students with specific score on all tests

Have a table for student test scores.
Students take multiple quizzes during a period.
Trying to develop a query that will pull only the students with 100% on all of the quizzes.
Table - Students
StudentId,
QuizScore
Certainly a student could receive 100% on a quiz or multiple quizzes, but just a list of the students whom scored 100% on all quizzes.
Thinking of a nested query but drawing a blank :(
Thanks in advance
Select studentid
From studentscores
Group by studentid
Having avg(quizscore) = 100
You can do an inner select to find students with at least one score that isn't 100, and then select all the StudentIDs that aren't in that set.
SELECT Distinct StudentID FROM Students WHERE StudentID NOT IN
(SELECT StudentID FROM Students WHERE QuizScore != 100)
You also want to use Distinct to get rid of multiple lines.
There are several ways to do this, one is to compare the count of all the results to those with 100. You can use case with count for that:
select studentid
from students
group by studentid
having count(case when quizscore >= 100 then 1 end) = count(*)
If quizes could have higher than 100, I've changed = to >= in the case statement to account for those. Otherwise, go with #Rajesh's solution as I believe it's the easiest to check for all 100s.

Access flag record when match found in table 2 (one-to-many relationship)

I have an educational db with a tbl_Students and tblStudentPrograms. The tblStudentPrograms has one record per student and program (ProgramID) per year (YearID.
I need to find out how many students participated in ProgramID=2 EVER. So, I need the DISTINCT subset of students who have participated in the program for any YearID.
(Of course, this will be complicated further by trying to find other records in other tables such as StudentAdvising as well , but this will be a good start.)
Thank you!
SELECT DISTINCT count(studentID) FROM tblStudentPrograms WHERE ProgramID = 2
Assuming you want a distinct count of students (excluding those where the same student may have taken a program twice...
SELECT count(distinct StudentID)
FROM tblStudentPRograms
WHERE ProgramID = 2
Assuming you want a distinct count of students (excluding those where the same student may have taken a program twice...
SELECT count(distinct StudentID)
FROM tblStudentPRograms
WHERE ProgramID = 2
though I'm not positive access supports a distinct w/in a count like other RDBMS do...
so you may have to do:
SQL : how can i count distinct record in MS ACCESS
SELECT count(BStudentID) as DistinctStudentsInProgram
FROM (select distinct studentID, ProgramID from tblStudentPrograms) B
WHERE B.ProgramID = 2

SQL Server query to know if an id in a column equals to other in another column

I’ve been hours trying to build this query and I need your help so I can make it.
This is table Students (made out of inner joins):
SpecialtyChosenID StudentID Subject SubjectSpecialtyID
5ABFB416-8137 15 Math A1EBF3CB-E899
5ABFB416-8137 15 English A1EBF3CB-E899
The info in it means that a student with id no. 15 has chosen an specialty with id 5ABFB416-8137
The two subjects he has passed (Math and English) belong to a specialty with id A1EBF3CB-E899
What would be the query to know if the passed subjects belong to the specialty chosen by the student??
Counting the number of subjects with the same SubjectSpecialtyID as SpecialtyChosenID and vice versa could do.
Thanks a lot
You can do a self join. This finds the number of subjects taken by the student that match the student's chosen specialities.
SELECT l.SpecialtyChosenID, l.StudentID, Count(Distinct r.Subject) FROM Students l
LEFT JOIN Students r ON (l.StudentID=r.StudentID AND l.SpecialityChosenID=r.SubjectSpecialityID)
GROUP BY l.SpecialtyChosenID, l.StudentID
However, this is quite inefficient using the table structure given. If you have a table listing students with their specialities, and another with subjects and specialities, and a third relating students with subjects, it would be better to build this query from the base data, rather than from the derived data.
SELECT * FROM Students WHERE SpecialtyChosenID = SubjectSpecialtyID
If you only need the list of matching subjects and you have the SpecialtyChosenID you can do something like
SELECT * FROM Students
WHERE SubjectSpecialtyID = SpecialityChosenID
CASE WHEN SpecialtyChosenID = SubjectSpecialtyID THEN 1 ELSE 0 END AS specialty

SQL - Add total from columns of two seperate tables

Complete novice here, trying to find out the total number of students from both the part-time students and full-time students and display the total in a named column.
partTimeStudents**(bannerID, moduleCode, modStartDate, rvisitorID)
fullTimeStudents**(bannerID, courseCode, crsStartDate, rvisitorID)
Thank you in advance for any help given :)
select
(select count(*) from partTimeStudents)+
(select count(*) from fullTimeStudents) as Total
I agree what others have said about the database design, but here's one example of a query that would fulfill your requirement.
SELECT SUM(students_count)
FROM (
SELECT COUNT(*) AS students_count
FROM partTimeStudents
UNION ALL
SELECT COUNT(*) AS students_count
FROM fullTimeStudents
)
You should not take 2 tables just to distinguish it by full/part time student. You can simply take a flag in table like:
students(bannerID, Code, modStartDate, rvisitorID, timeFlag)
In timeFlag you can manage whether the student is full time or part time.
Now to get count of all students[full time + part time]:
select count(*) from students;
And to have counts for fullTime students or partime students:
select count(*) from students where tiemFlag=1; //--- assuming 1 is for fulltime
select count(*) from students where tiemFlag=0; //--- assuming 0 is for parttime