I have two tables: subject and student.
I'm trying to count the number of subjects enrolled by each student. How do I do that? I'm trying the code below but it doesn't give the answer I need.
SELECT COUNT( subject.SUBJECT ) , student.IDNO, student.FIRSTNAME, subject.SUBJECT
FROM student, subject
GROUP BY subject.SUBJECT
LIMIT 0 , 30
you do have an Enrolment table too, don't you? to resolve the many-to-many relationship between student and subject. you could work with just that. say, your enrolment table has studentID and subjectID columns, then you would only need:
SELECT studentID, COUNT(*) FROM Enrolment GROUP BY studentId;
or, to include their names too,
SELECT s.firstname, COUNT(*) FROM Enrolment e JOIN student s on e.studentId=s.IDNO GROUP BY e.studentId;
You are missing a join condition. Somewhere you are storing info about what subject each student is taking. Perhaps WHERE subject.student_id = student.id.
Related
I am a beginner with SQL syntax. I have 2 tables called STUDENT and EXAMINATION and I am, in 1 line, trying to write a query to print ID, SUBJECT and NUMBER_OF_TIMES. I am not told which column belongs to which table but my guess is that ID belongs to STUDENT and SUBJECT belongs to EXAMINATION. NUMBER_OF_TIMES is just how many times the same student has taken that subject exam.
I tried SELECT STUDENT.ID, EXAMINATION.SUBJECT, NUMBER_OF_TIMES but no luck.
Your working query would look something like this:
SELECT
s.ID,
e.SUBJECT,
COUNT(e.STUDENT_ID) AS cnt
FROM STUDENT s
LEFT JOIN EXAMINATION e
ON s.ID = e.STUDENT_ID
GROUP BY
s.ID,
e.SUBJECT
This answer assumes, really out of necessity, that there exists a join column STUDENT_ID in the exam table, which connects with the student table. We use a left join so as to report all students, even those with a zero count.
I have table consists of student number, subject, exam_date, and I can find the students who have more than one exam by using this query
SELECT
student_number, subject, exam_date
FROM
tables
WHERE
dates ='01-01-2019' And
student_number IN (
SELECT student_number
FROM tables WHERE dates ="01-01-2019"
GROUP BY student_number
HAVING COUNT(student_number) > 1
)
However, I want to find all students who have more than one exams on the same date for the whole period of the exams (date list) instead of typing each date individually.
You are pretty close with your group by.
SELECT student_number, exam_date
FROM tables
GROUP BY student_number, exam_date
HAVING count(*)>1
This should get you started, you had the right idea with your GROUP BY...
I guess if a student has more than one exams on the same date, these exams would be on different subjects.
So you can use get what you need with EXISTS:
SELECT student_number, subject, exam_date
FROM tables t
WHERE EXISTS (
SELECT 1 FROM tables
WHERE
student_number = t.student_number
AND
exam_date = t.exam_date
AND
subject <> t.subject
)
This is a homework question. I am not asking for the correct answer, I am just looking for help whether I am going in the right direction.
The prompt is that I am to return the first and last name of students who either (OR): (a) Is not currently enrolled (not in Enrollments table) or (b) has the lowest score in any class within their own department.
The database schema is as follows:
My SQL query that I came up with is:
SELECT FIRSTNAME, LASTNAME
FROM STUDENTS
JOIN ENROLLMENTS ON STUDENTS.NETID = ENROLLMENTS.NETID
JOIN COURSES ON COURSES.CRN = ENROLLMENTS.CRN
WHERE STUDENTS.NETID NOT IN (
SELECT NETID
FROM ENROLLMENTS
) OR
ENROLLMENTS.SCORE IN (
SELECT MIN(SCORE)
FROM ENROLLMENTS
WHERE COURSES.DEPARTMENT = STUDENTS.DEPARTMENT
GROUP BY ENROLLMENTS.CRN
);
I tried to use JOIN clauses to combine the three tables where they intersect. The NetId is distinct to each student, identifying them in the Enrollments table. Each class has a unique CRN so I connected them there between Courses table and Enrollments table.
I do not know where the problem lies but the expected outcome is:
Wbixik Yjepuriluwe
Wtoyi Avamijosu
Jheyiresoxo Bsexedoh
Ulerusota Mzuzu
But my outcome is:
Wbixik Yjepuriluwe
Jropop Vduyumi
Jheyiresoxo Bsexedoh
Ulerusota Mzuzu
Looking for any guidance to get me back on track.
You can try using left join
SELECT FIRSTNAME, LASTNAME
FROM STUDENTS
left JOIN ENROLLMENTS ON STUDENTS.NETID = ENROLLMENTS.NETID
JOIN COURSES ON COURSES.CRN = ENROLLMENTS.CRN
where ENROLLMENTS.NETID is null OR
ENROLLMENTS.SCORE IN
(SELECT MIN(SCORE) FROM ENROLLMENTS
WHERE COURSES.DEPARTMENT = STUDENTS.DEPARTMENT
GROUP BY ENROLLMENTS.CRN
)
I have the two tables STUDENT and TaskEffort.
Many students have worked on same tasks. For a particular task the name of the students and effort should be considered.
The STUDENT table contains studentid, firstName and lastName.
The TaskEffort table contains taskid, studentid and Effort
I need to display the taskid, first name, last name, effort, for those who worked on a particular task.
This is one of the queries I tried, but this is not working.
SELECT t.id, s.firstname, s.lastname, t.effort
FROM taskeffort t
LEFT OUTER JOIN student s ON t.id = 4 AND s.studentid = t.studentid
Thanks in advance.
First the design is not accurate.
You said that "Many students must have worked on same task" but your design is
1-many students. It means that each task has only one student. You need to change the design to support you requirements.
this relation is many-to-many. Student can be assigned to many tasks and tasks may have been assigned to many students.
Add a third table called taskAssignments with columns: student_id,task_id and remove student_id column from TaskEffort.
Then run this query:
SELECT t.id, s.firstname, s.lastname, t.effort
FROM TaskEffort t
LEFT JOIN taskAssignments ta ON t.id=ta.task_id
LEFT JOIN student s ON s.studentid = ta.studentid
WHERE t.id = 4
SELECT t.id, s.firstname,s.lastname, t.effort
FROM taskeffort t
LEFT OUTER JOIN student s ON s.studentid = t.studentid
WHERE t.id = 4;
i have a table with all the students names and another table with student name and their enrollments in classes i want to show both students with their class names even if they are not enrolled in any classes i used union between the enrolled and not enrolled the only problem that the names are duplicated
Student (student_id, student_name, major, level, age)
Enrolled (student_id, class_name, semester)
and here's the MySQL query:
select student_name,class_name from student natural join enrolled union select student_name,null as class_name from student group by student_name;
Does this work for you?
SELECT
s.student_name,
e.class_name
FROM
student s
LEFT JOIN enrolled e ON s.student_id = e.student_id
GROUP BY student_name;
LEFT JOIN is used when one of the tables can have null values, whilst the other can not. The "left" table (student in this case) is the most important one, the "right" table (enrollment) is the one that can be null.
So it will ALWAYS find the student tables, but when it can't find a class_name that's linked to that student, it just shows up as NULL.
If you want multiple enrollments on the same student, you'll have to remove the group by.
You should use LEFT JOIN like so: SELECT s.student_name, e.class_name from student s LEFT JOIN enrolled e ON s.student_id=e.student_id