Select the records where ID is in different table - sql

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

Related

Sql select clause with three table

I am trying to write a sql query to show student list for each course.
The diagram below show the database relationship.
The SQL query I have written is:
select * from Courses
inner join Enrollments on Enrollments.CourseId = Courses.CourseId
inner join Student on Enrollments.StudentId = StudentId
where Courses.CourseId = 1
The issue is that i am getting returned alot more data than I expected as only one student is registered for the course but i get ten entries. I am not sure if i have done somethings fundamental wrong or is my query the issue.
This is the data
This is the result
I expected only two rows to be returned.
Thanks
Every column in your query must be qualified with the table's name.
You did not qualify the column StudentId in this join:
inner join Student on Enrollments.StudentId = StudentId
If you did you would find the error which is that there is no column StudentId in the table Student and you should use the column Id:
select * from Courses
inner join Enrollments on Enrollments.CourseId = Courses.CourseId
inner join Student on Enrollments.StudentId = Student.Id
where Courses.CourseId = 1
or better with aliases for the tables:
select *
from Courses as c
inner join Enrollments as e on e.CourseId = c.CourseId
inner join Student as s on e.StudentId = s.Id
where c.CourseId = 1
The primary key of table Student is Id, not StudentId.
So the correct query is:
select * from Courses
inner join Enrollments on Enrollments.CourseId = Courses.CourseId
inner join Student on Enrollments.StudentId = Student.Id
where Courses.CourseId = 1

sqlite: count and group by clause gives not the result expected

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.

SQL Selecting from multiple tables

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'

How to get common category among columns in SQL

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

mysql - three joins on the same table

I have two tables:
persons
- person_id
- fullname
students
- student_id
_ person_id
- father_id
- mother_id
In students table the last three columns store ids from persons table. What SELECT could retrieve the following data:
- student name
- father name
- mother name
We assume no WHERE, ORDER BY, or LIMIT for simplicity
try this:
select sp.fullname studentname, fp.fullname fathername, mp.fullname mothername
from students s
inner join persons sp on (s.student_id = sp.person_id)
inner join persons fp on (s.father_id = fp.person_id)
inner join persons mp on (s.mother_id = mp.person_id)
Try below query -
SELECT p.fullname,m.fullname,f.fullname from students s
LEFT JOIN persons p ON s.person_id = p.id
LEFT JOIN mother m ON s.mother_id = m.id
LEFT JOIN father f ON s.father_id = f.id
WHERE s.student_id = 'id of which student record you want';