SQL Selecting from multiple tables - sql

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'

Related

Write the SQL code that will list Physician-Person appointments only ONCE

This one is confusing me?
There are three tables. Appointments(Appointment_ID, Physician_ID and Person_ID) , Physician(Physician_ID) and a Person(Person_ID and Physician_ID).
This is what I have so far :
SELECT DISTINCT Appointment_date_time FROM Appointment
INNER JOIN Person
ON Appointment.Person_ID = Person.Person_ID
INNER JOIN Physician
ON Physician.Physician_ID = Person.Physician_ID
HAVING COUNT(*) < 1
There are three tables. Appointments(Appointment_ID, Physician_ID and Person_ID) , Physician(Physician_ID) and a Person(Person_ID and Physician_ID).
select *
from Appointments a
inner join Person p
on a.Person_ID = p.Person_ID
inner join Physician ph
on a.Physician_ID = ph.Physician_ID

Select the records where ID is in different table

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

SQL Join operation in multiple tables

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.

sql self join problems how to find the duplicates from same table

I am student and
I have such kind of a table structures
student (sid, sname, gender, age, year, gpa)
dept(dname, numphds)
course(cno, cname, dname)
enroll(sid, grade, dname, cno, sectno)
and i want the results for.....
Q-Print the names of students who are taking both a "Computer Sciences" course and a "Maths" course................
I have tried following query but it does not give me throughput..............
select e.sid, s.sname from
enroll e ,enroll e1 ,student s ,course c
where s.sid=e.sid,
e.cno =c.cno
and e.sid=e1.sid
and c.cname='computer science' and c.name='maths';
It's been more than 20 years since proper joins were introduced to SQL. FFS use them!
Try this:
select s.sid, s.sname
from student s
join enroll e on e.sid = s.sid
join course c on c.cno = e.cno and c.cname='computer science'
join enroll e1 on e1.sid = s.sid
join course c1 on c1.cno = e1.cno and c1.name='maths'
Note how you may put non-key conditions in a join condition. Although the query would still work if the course names were in a where clause, putting the conditions in the join clause makes it more obvious where they apply, which improves readability.
By formatting well and ordering the tables sanely, you can actually read the query and (hopefully) understand it.
use the following code
select e.said, s.sname from enroll e inner join
enrool e1 on e1.sid = e.sid inner join
student s on s.sid = e.sid inner join
course c on c.cno = e.cno
where c.cname = 'computer science' and c.name = 'maths'
if you need to use the left join then use
select e.said, s.sname from enroll e left join
enrool e1 on e1.sid = e.sid left join
student s on s.sid = e.sid left join
course c on c.cno = e.cno
where c.cname = 'computer science' and c.name = 'maths'

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