I have three tables in MS Access as follow:
--Students(ID, Name, Class)
--Subjects (ID, Name)
--Marks (ID, StudentID, Subject.ID)
Relation is as follow:
Marks.Subject = Subjects.ID
Marks.StudentID = Students.ID
Please Help me write a query that can return Name of Students and All SubjectNames and Marks of that student.
Currently I have this query but it returns marks separately.
select Students.Name, Marks.Obtained, Subjects.Name from Marks
inner join Students on Marks.StudentName = Students.ID
You have joined students and marks table with that Join Subjects table too
SELECT students.NAME,
marks.obtained,
subjects.NAME
FROM ( marks
INNER JOIN students
ON marks.studentname = students.id )
INNER JOIN subjects
ON marks.subject = subjects.id
This will help you :
select Students.Name, Marks.Obtained, Subjects.Name from Marks
inner join Students on Marks.StudentName = Students.ID
inner join Subjects on Marks.Subject = Subjects.ID;
try this:
select Students.Name, Marks.Obtained, Subjects.Name from Marks
inner join Students on Marks.Subject = Subjects.ID
Marks.StudentID = Students.ID
Try this:
SELECT a.name, b.obtained, c.name
FROM studentTable a
INNER JOIN marksTable b ON a.ID = b.StudentID
INNER JOIN subjectsTable c ON b.Subject.ID = c.ID
I would reorder this as follows:
select A1.Name as 'Student Name'
, A3.Name as 'Subject'
, A2.Obtained as 'Mark Obtained'
from Students AS A1
inner join Marks as A2 on A1.ID = A2.StudentID
inner join Subjects AS A3 on A2.Subject = A3.ID GO;
I hope that helps.
Related
I have tried using group by and getting an error message of single row function cannot return multiple values.
It has three tables to be selected student, subject and mark.
If you only required student_id and MAX number, you can use only tables Student and Marks as below-
SELECT A.stident_id,MAX(B.Value) max_marks
FROM Student A
INNER JOIN Mark B ON A.Student_id = B.Student_id
GROUP BY A.stident_id
But if you need subject name as well, you can try this below logic-
SELECT AA.stident_id,AA.stident_name,
D.Subject_name,AA.max_marks
FROM
(
SELECT A.stident_id,A.stident_name,MAX(B.Value) max_marks
FROM Student A
INNER JOIN Mark B ON A.Student_id = B.Student_id
GROUP BY A.stident_id
)AA
INNER JOIN Marks C ON AA.stident_id = C.stident_id
AND AA.max_marks = C.Value
INNER JOIN Subject D ON C.subject_id = D.subject_id
on sqlite, I have the tables
papers: rero_id, doi, year
writtenby: rero_id, authorid, instid
authors: author_id, name, firstname
inst: inst_id, name, see_id
inst is a table of Institutions: Universities and so on.
Each line in writtenby gives a paper, an author, an institution this author was attached at that time. There can be more then one institution and the couple paper, authorid is repeated for each institution.
For a given author, I want a list and a count of the institutions he has cohautored paper with.
For a list I tried
SELECT inst.name as loc
FROM (
(authors INNER JOIN writtenby ON authors.authorid =
writtenby.authorid)
INNER JOIN writtenby AS writtenby_1 ON writtenby.rero_id =
writtenby_1.rero_id
)
INNER JOIN authors AS auth_1 ON writtenby_1.authorid =
auth_1.authorid
inner join inst on writtenby_1.instid = inst.inst_id
WHERE (authors.name) ="Doe" AND (authors.firstname)= "Joe"
ORDER BY loc
I got a list that seems ok.
Now, I would like to regroup these institution names and have a count.
I tried
SELECT inst.name, count(inst.name)
FROM (
(authors INNER JOIN writtenby ON authors.authorid =
writtenby.authorid)
INNER JOIN writtenby AS writtenby_1 ON writtenby.rero_id =
writtenby_1.rero_id
)
INNER JOIN authors AS auth_1 ON writtenby_1.authorid =
auth_1.authorid
inner join inst on writtenby_1.instid = inst.inst_id
GROUP BY inst.name
HAVING (authors.name) ="Doe" AND (authors.firstname)= "John"
I have only three line and not a count of the institutions listed from the first query.
Thanks for correcting me !
François
Try using where instead of having
SELECT inst.name, count(inst.name)
FROM (
(authors INNER JOIN writtenby ON authors.authorid =
writtenby.authorid)
INNER JOIN writtenby AS writtenby_1 ON writtenby.rero_id =
writtenby_1.rero_id
)
INNER JOIN authors AS auth_1 ON writtenby_1.authorid =
auth_1.authorid
inner join inst on writtenby_1.instid = inst.inst_id
where authors.name ='Doe' AND authors.firstname= 'John'
GROUP BY inst.name
I got this that works,
SELECT inst.name as loc, count(*) as c
FROM (
(authors INNER JOIN writtenby ON authors.authorid = writtenby.authorid)
INNER JOIN writtenby AS writtenby_1 ON writtenby.rero_id =
writtenby_1.rero_id
inner join inst on writtenby_1.instid = inst.inst_id
)
INNER JOIN authors AS auth_1 ON writtenby_1.authorid = auth_1.authorid
WHERE (authors.name) ="Doe" AND (authors.firstname)= "John"
GROUP BY inst.name
ORDER BY c DESC
I still can use a where clause, and that's not the same as having...
And thanks to fa6 who gave the answer below
F.
I have 3 tables. Below is the structure:
Student : SID,SNAME
Subject : SUID,SUNAME
Rid : SID,SUID
the result of the query should be :
SNAME SUNAME
Try this:
select st.SNAME
, sj.SUNAME
from Rid r
inner join Student st on r.SID = st.SID
inner join Subject sj on r.SUID = sj.SUID
Use this one:
select st.SNAME, sj.SUNAME
from Rid r
left join Student st on r.SID = st.SID
left join Subject sj on r.SUID = j.SUID
You have two tables and a relationship table. The relationship (Rid) table is the one that relates the other two (Student and Subject). You must search the Rid records where the Student and the Subject are joined:
SELECT s.sname, sb.suname
FROM student s, subject sb, rid r
WHERE s.sid = r.sid AND sb.suid = r.suid;
or with New Style
SELECT s.SNAME, sb.SUNAME
FROM Rid r
INNER JOIN Student s on r.SID = s.SID
INNER JOIN Subject sb on r.SUID = sb.SUID
I have four tables Student,Enrolment,Building,Campus and their fields are as:
Student:
StudentID
Name
Level
Enrolment:
Ref
StudentID
Course
EnrolDate
Building_ID
Building:
BuildingID
BuildingName
CampusID
Campus:
CampusID
CampusName
I need Name of students who are enrolled and studying at the CampusName = 'City Centre'. I tried numerous things but because it needs multiple connections to different tables I got really confused.
Thank you
Something like this:
SELECT S.Name
FROM Student S
INNER JOIN Enrolment E ON S.StudentID = E.StudentID
INNER JOIN Building B ON E.Building_ID = B.BuildingID
INNER JOIN Campus C ON C.CampusID = B.CampusID
WHERE C.CampusName = 'City Centre'
Just do the joins in order -- left to right:
SELECT *
FROM Student S
JOIN Enrolment E ON E.StudentID = S.StudentID
JOIN Building B ON B.BuildingID = E.Building_ID
JOIN Campus C ON C.CampusID = B.CampusID
WHERE C.CampusName = 'City Centre'
Try this:
SELECT S.*
FROM Students S INNER JOIN
Enrolment E ON E.StudentID=S.StudentID INNER JOIN
Building B ON B.BuildingID= E.Building_ID INNER JOIN
Campus C ON C.CampusID=E.CampusID
WHERE CampusName = 'City Centre'
Consider the following schema for SQL:
Student (StudID, StudName, DeptID, Age, Gpa);
Course (CourseID, CourseName, InstructorID);
Department (DeptID, DeptName, Location);
Instructor (InstructorID, InstructorName, DeptID);
Section (SectionID, SectionName, Time, RoomID, CourseID, InstructorID);
Room (RoomID, RoomName, Location);
Enrolled (StudID, SectionID);
Q: How to find the names of all sections that either meet in common room or have five or more students enrolled?
Well I am not sure if it will work:)
Common names;
select st.StudName as names from Student as st inner join Departmant as d
on st.DeptID = d.DeptID inner join Instructor as i
on i.DeptID = d.DeptID inner join Course as c
on c.InstructorID = i.InstructorID inner join Section as s
on s.InstructorID = i.InstructorID inner join Room as r
on r.RoomID = s.RoomID inner join Enrolled as e
on e.StudID = st.StudID;
More than 5 student enrolled(Something experimental:);
select st.StudName as names from Student as st inner join Departmant as d
on st.DeptID = d.DeptID inner join Instructor as i
on i.DeptID = d.DeptID inner join Course as c
on c.InstructorID = i.InstructorID inner join Section as s
on s.InstructorID = i.InstructorID inner join Room as r
on r.RoomID = s.RoomID inner join Enrolled as e
on e.StudID = st.StudID where count(e.StudID = st.StudID)>4;
If you are using SQL-Server you can write the query like this(I didn't tested it. So I can't say it works 100% but I hope it gives you an idea):
SELECT SectionName
FROM Section
WHERE SectionID IN --Students in common room.
(
SELECT SectionID FROM Section
INNER JOIN Instructor ON Section.SectionID = Instructor.InstructorID --Section to which the Instructor belongs
INNER JOIN Department ON Department.DeptID = Instructor.DeptID --Department to which the Instructor belongs
INNER JOIN Room ON Room.Location = Department.Location --Room to which the Department belongs
)
OR --Student Enrollment greater than 5.
(
(SELECT COUNT(StudID) FROM Student
INNER JOIN Enrolled ON Student.StudID = Enrolled.StudID
INNER JOIN Section ON Section.SectionID = Enrolled.SectionID) >= 5
)
Q:6. Find the names of all sections that either meet in room New-8 or have five or more students enrolled.
Answer:
select sectionName
from student as S, Enrolled as E, Section as Se
where S.studId=E.studId AND E.sectionID=se.SectionID
group by sectionName
having count(*)>=3
UNION
SELECT sectionName
FROM SECTION S, ROOM R
WHERE S.RoomID=R.ROOMID AND R.RoomName='new2'
this is the correct query i have run it on Db