I couldn't end up retrieving data from multiple tables in a single query.
I have the following tables:
A brief explanation :
A student can study many subjects and subjects can be studied by many students (table Study Created)
A subejct is taught by a lecturer (FK used in (table Subject))
The table Grade gives infos about the marks obtained by a student
The SQL query I was trying to perfom is: Retrieve average students who study subjects taught by lecturer Antonio.
So first I used an AVG aggregation on table grade to get average students:
SELECT Id_Student, Name, AVG(Mark)
AS Average FROM Grade
GROUP BY Id_Student, Name
But my trouble is also to use the result of this SQL query and filter with again with the result of Subjects taught by Lecturer "Antonio".
Is it possible to make it in a one SQL query?
SELECT Id_Student, Name, AVG(Mark) AS Average FROM Grade
where id_student in
(
select student.id_student from subject
join study on subject.id_subject = study.id_subject
join student on study.id_student = student.id_student
where subject.taught_by = L1
)
GROUP BY Id_Student, Name
also you could probably just join GRADE instead of doing the subselect that I did here. But it's hard to tell how it will go without actually testing it.
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.
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
)
Using a university relation (students are advised by instructors in different departments, only one advisor per student but advisors can have zero to many advisees), I'm attempting to write a function that computes the total number of students being advised in a given department.
Here are the tables and columns for reference:
student(id,name,dept_name,tot_cred)
instructor(id,name,dept_name, salary)
advisor(s_id,i_id)
I already know how to use delimiter and such so no I don't need to be told how to write a function. I'm just having issues getting what I want from the select statement alone.
This is the best I've been able to come up with so far:
SELECT *
FROM advisor
RIGHT OUTER JOIN instructor
ON i_id=id
ORDER BY dept_name;
This statement yields:
Is there a way I can write an if statement or some other statement to delete all of the NULL entries and then use a count function (within the custom function I'll be writing) that will count the number of instances dept_name occurs, resulting in the total number of advisees per department?
SELECT i.dept_name, COUNT(a.s_id)
FROM Instructor i
LEFT JOIN advisor a ON i.id = a.i_id
GROUP BY i.dept_name;
This will include zeros for departments with no advisees.
If you are interested in the number of advised students per student department then:
SELECT s.dept_name, COUNT(a.s_id)
FROM student s
LEFT JOIN advisor a ON a.s_id=s.id
GROUP BY s.dept_name
OTOH, if you are interested in the number of advised students per instructor department then:
SELECT i.dept_name, COUNT(a.s_id)
FROM instructor i
LEFT JOIN advisor a ON a.i_id=i.id
GROUP BY i.dept_name
It looks like this is what you need:
SELECT i.dept_name, COUNT(a.s_id) FROM instructor i
LEFT OUTER JOIN advisor a ON i.id = a.i_id
GROUP BY i.dept_name;
I have to add that advisor(s_id,i_id) is bad database design if a student really can not have more than 1 advisor as you said. Because in that case, simply add advisor_id to the student relation and get rid of the advisor table.
Also: your use of the word 'function' is incorrect. We are talking about queries here.
Probably you can try this:
SELECT i.dept_name, COUNT(s.*) AS totalAdvisedNum
FROM student s, advisor a, instructor i
WHERE a.s_id = s.id AND a.i_id = i.id GROUP BY i.dept_name
This will select the department name and total number of students being advised by instructors in these departments
If you need to get the number for a specific department you should replace GROUP BY i.dept_name with AND i.dept_name = "name of department"
I have a table that tracks attendance in a course. The columns are the courseid, lesson, personid, and date. I have a query (below) that extracts the earliest date a person appears along with the associated course, lesson, and personid. This is used to determine when a person started a particular course and ensure they started with the first lesson. This works fine, but where I am stuck is running this query per course. For example, finding the first date each person in a particular course started it rather than for every course. Right now I am just running the more general query and filtering it in the biz layer.
I obfuscated this a bit so forgive any typos:
select a.courseid,
a.lesson,
a.personid,
a.thedate
from (select personid,
min(thedate) as earliestdate
from attendance
group by personid) as x
inner join attendance as a on (a.personid = x.personid and a.thedate = x.thedate)
Just group over person_id, course in the inner query:
select a.courseid, a.lesson, a.personid, a.thedate
from (
select personid, courseid, min(thedate) as earliestdate
from attendance
group by personid, courseid
) as x
inner join attendance as a
on (a.personid = x.personid and
a.thedate = x.thedate and
a.courseid=x.course_id)
I have a small doubt here. Currently all the information is maintained in single data object/table. How would it be if we have different data model like below...?
Objext1:
Course table: Course details having lession with a relation
Student table: Contains student details.
Will the querying would be simplified in this way....?
Sorry if anything sounds immatur...
Regards,
UDAY
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.