How to get common category among columns in SQL - 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

Related

SQL: Need to check columns for values that exist in another column

Using SQL, my job is to fetch the SSN of students who enrolled in a course without enrolling in that course’s prerequisite(s). I'm using Access. The tables I need are as follows:
STUDENT (SSN, SNAME, MAJOR, DOB, ADDRESS)
ENROLLED (SSN, CID, GRADE)
PREQ (CID, PREQCID, PASSINGGRADE, NOTE)
Here's what I've done so far.
select *
from
(select SSN, CID from ENROLLED) AS enrolled
left join
(select CID, PREQCID FROM PREQ) AS prereq ON enrolled.CID = prereq.CID;
What I'm missing is how to check each row of the same student on the condition WHERE enrolled.CID = prereq.CID for a PREQCID that's NOT in enrolled.CID.
Is what I'm asking for here a loop? Am I on the right track? Please keep in mind this I'm in an introductory course so the simplest of solutions is preferable.
Here's another way using not exists:
select e1.ssn
from enrolled e1 left join preq p on e1.cid = p.cid
where
p.preqcid is not null and
not exists (select 1 from enrolled e2 where e2.ssn = e1.ssn and e2.cid = p.preqcid)
This is essentially stating:
"Select the ssn for all enrollments where there is a prerequisite course and the prerequisite course ID does not exist in the table of enrollments for that ssn."
I use select 1 purely for optimisation - we don't care about the values held by the nested query, only whether or not the nested query returns one or more records.
You could also write this using joins as:
select e1.ssn
from
(
enrolled e1 left join preq p on e1.cid = p.cid
)
left join enrolled e2 on
p.preqcid = e2.cid and e1.ssn = e2.ssn
where
e2.cid is null
Here, the enrolled table is referenced twice: the first is left joined on the table of prerequisite courses for each course, and the second is left joined on the prerequisite course ID and the ssn from the original enrollment.
The where clause then causes records to be selected for which the link on the prerequisite course is null for the given ssn.
I'm sure there is a cleaner way to do this, but this gets you your result:
Select S.SSN, E.CID From Student S
Inner Join Enrolled E on E.SSN = S.SSN
Where E.CID IN (Select CID From PREQ Where Preqcid NOT IN
(Select CID From Enbrolled Where SSN = S.SSN))
This might work if analytic functions and CTEs are available:
with d as (
select ssn, cid,
count(distinct p.cid) over (partition by e.ssn, e.cid) ecnt, ,
count(distinct e.cid) over (partition by e.ssn, p.cid) pcnt
from enrolled e left outer join preq p on p.cid = e.cid
)
select distinct ssn, cid from d
where ecnt = pcnt;

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 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'

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'

INNER JOIN to more than one table

I have four tables
student
-------
id, int
firstName, varchar
lastName, varchar
course
------
id, int
name, varchar
semester
--------
id, int
name, varchar
mark
----
studentId, int
courseId, int
semesterId, int
mark, int
I want to make a sql query that retrives firstName, lastName, courseName, semesterName and mark for every row in the mark table.
I tried to do it with INNER JOIN but I can't make INNER JOIN for more than one table.
That code I reached finally
select student.firstName, student.lastName, course.name, semester.name, mark.mark
from mark
INNER JOIN student ON student.id = mark.studentId
INNER JOIN course ON course.id = mark.courseId
INNER JOIN semester ON semester.id = mark.semesterId
In Ms Access you have to include parentheses in a query with multiple joins:
select st.firstName, st.lastName, c.name, sm.name, m.mark
from (((mark m
INNER JOIN student st ON st.id = m.studentId)
INNER JOIN course c ON c.id = m.courseId)
INNER JOIN semester sm ON sm.id = m.semesterId)
Try theta style join:
select student.firstName, student.lastName, course.name, semester.name, mark.mark
from mark, student ,course, semester
WHERE student.id = mark.studentId AND course.id = mark.courseId AND semester.id = mark.semesterId