SQL Select with multiple conditions - sql

I have 3 tables:
The question is "List every course number in which both ‘John Smith’ and ‘Kevin Miller’ are enrolled."
My query:
SELECT DISTINCT CourseNumber
FROM Enrollment
INNER JOIN Student ON Student.SSN = Enrollment.SSN
WHERE Student.Name = 'John Smith'
AND Student.Name = 'Kevin Miller'
but it didn't return anything.
If I change the operator "AND" to "OR", it's going to show 2 courses number which is the wrong answer.
Can anybody help me please? Thank you

You need some kind of aggregation logic here, because the assertion on both students inherently involves more than one record. So, a WHERE clause by itself won't work. Here is one common way to approach this:
SELECT e.CourseNumber
FROM Enrollment e
INNER JOIN Student s ON s.SSN = e.SSN
WHERE s.Name = IN ('John Smith', 'Kevin Miller')
GROUP BY e.CourseNumber
HAVING MIN(s.Name) <> MAX(s.Name);
I prefer the above approach, but you could also use exists logic here:
SELECT DISTINCT e.CourseNumber
FROM Enrollment e
INNER JOIN Student s ON s.SSN = e.SSN
WHERE
e.Name = 'John Smith' AND
EXISTS (SELECT 1 FROM Enrollment e1
INNER JOIN Student s1 ON s1.SSN = e1.SSN
WHERE e1.CourseNumber = e.CourseNumber AND
s1.Name = 'Kevin Miller');

Just to show an alternative to Tim's answer: A simple approach is INTERSECT.
SELECT coursenumber FROM enrollment WHERE ssn = (SELECT ssn FROM Student WHERE name = 'John Smith')
INTERSECT
SELECT coursenumber FROM enrollment WHERE ssn = (SELECT ssn FROM Student WHERE name = 'Kevin Miller')
There are some DBMS that don't support INTERSECT, though.

Related

'ALL' concept in SQL queries

Relational Schema:
Students (**sid**, name, age, major)
Courses (**cid**, name)
Enrollment (**sid**, **cid**, year, term, grade)
Write a SQL query that returns the name of the students who took all courses.I'm not sure how I capture the concept of 'ALL' in a SQL query.
EDIT:
I want to be able write it without aggregation as I want to use the same logic for writing the query in relational algebra as well.
Thanks for the help!
One way of writing such queries is to count the number of course and number of courses each student took, and compare them:
SELECT s.*
FROM students s
JOIN (SELECT sid, COUNT(DISTINCT cid) AS student_courses
FROM enrollment
GROUP BY sid) e ON s.sid = e.sid
JOIN (SELECT COUNT(*) AS cnt
FROM courses) c ON cnt = student_cursed
This gives course combinations that are possible but haven't been taken...
SELECT s.sid, c.cid FROM students CROSS JOIN courses
EXCEPT
SELECT sid, cid FROM enrollment
So, you can then do the same with the student list...
SELECT sid FROM students
EXCEPT
(
SELECT DISTINCT
sid
FROM
(
SELECT s.sid, c.cid FROM students CROSS JOIN courses
EXCEPT
SELECT sid, cid FROM enrollment
)
AS not_enrolled
)
AS slacker_students
I don't like it, but it avoids aggregation...
SELECT *
FROM Students
WHERE NOT EXISTS (
SELECT 1 FROM Courses
LEFT OUTER JOIN Enrollment ON Courses.cid = Enrollment.cid
AND Enrollment.sid = Students.sid
WHERE Enrollment.sid IS NULL
)
btw. names of tables should be in singular form, not plural

SQL: Find common rows in different record

I have 3 tables:
Teacher Table (t_id, email, ...)
Student Table (s_id, email, ...)
Teaching Table (t_id, s_id, class_time, ...)
I have a task which is, given two t_id, find the common students that these 2 teachers have taught.
Is it possible to accomplish this in strictly SQL? If not I might try to retrieve out the student records individually based on different teacher, and do a search to see which students they have in common. This seems a bit overkill for something that seems possible to write a SQL query for.
You can self join to get students for both teachers.
DECLARE #TeacherID1 INT = 1
DECLARE #TeacherID2 INT = 2
SELECT
StudentID = T1.s_id,
Teacher1 = T1.t_id,
Teacher1ClassTime = T1.class_time ,
Teacher2 = T2.t_id,
Teacher2ClassTime = T2.class_time
FROM
TeachingTable T1
INNER JOIN TeachingTable T2 ON T2.s_id=T1._sid AND T2.t_id=#TeacherID2
WHERE
T1.t_id = #TeacherID1
ORDER BY
T1.ClassTime
select s_id
from student a
inner join teaching b on a.s_id = b.s_id
where t_id = 'First give t_id'
INTERSECT
select s_id
from student a
inner join teaching b on a.s_id = b.s_id
where t_id = 'Second give t_id'
This work with MS DB, but probably not with others.
select s_id
from student a
inner join teaching b on a.s_id = b.s_id
where b.t_id = 'First give t_id'
and s_id in (
select s_id
from student c
inner join teaching d on c.s_id = d.s_id
where d.t_id = 'Second give t_id'
)
the second one should work with any DB.

SQL query, NOT EXISTS

Hi I am evaluating a SQL query with the following schema:
Student(sid, name, age, gender, dept, GPA)
Faculty(fid, name, age, office, dept)
Course(cid, name, description)
Teach(fid, cid, term)
Enrollment(sid, cid, term, grade, final grade)
The question to base query on is:
Find the students who took at least one course with Tom
The query looks like:
SELECT *
FROM Student S1
WHERE NOT EXISTS (SELECT E1.cid
FROM Enrolment E1, Student S2
WHERE E1.sid = S2.sid
AND S2.name = 'Tom'
MINUS
SELECT E2.Cid
FROM E2 Enrollment
WHERE E2.sid = S1.sid)
This query is not making sense to me because to me it seems like the NOT EXISTS condition would only select a student when the subquery is empty, and this would only occur in the case when student s1 has taken all the same courses as Tom. However the question is to find the students who took at least one course, not all courses as Tom. Maybe I am interpreting wrong but I could use some clarification.
Try this
DECLARE #TomID nvarchar(MAX)
set #TomID = (SELECT TOP 1 ID from Students where Name = 'Tom')
select distinct E1.SID from Enrollment E1
where E1.SID != #TomID AND E1.CID in
(select distinct E.CID from Enrollment E
where E.SID=#TomID)

SQL Join left join or left outer join

I am having a question in SQL Joins. I have table employee with employeeid as primary key and some other columns for employee. And there is another table called employeeaddress where there can be multiple employeeid is a foreign key. One employee can have many employeeaddresses just to explain one to many relationship.
If I want to write a query which will fetch the following columns
employee.employeeid, employee.empname,
employeeaddress.employeeaddressid, employeeaddress.addr1,
employeeaddress.addr2
So there can be an employee with no employeeaddress. But anyway I wanted to fetch all the employees who may have zero or multiple addresses.
Do I need to apply left join or left outer join? I want the following result for a table that has 2 employees John and Michael where John has two employeeaddresses with employeeaddressid 21 and 22 and Michael has no employeeaddress
1, John, 21, addr1 for John, addr2 for John
1, John, 22, another addr1 for John, another addr2 for John
2, Michael, NULL , NULL , NULL
The above result is arranged in the following fashion
employee.employeeid, employee.empname, employeeaddress.employeeaddressid, employeeaddress.addr1, employeeaddress.addr2
Please help.
Based on your description it sounds like you're looking for a query as follows. If you also wanted the address details, you'll just have to add a left join to the outer query.
Also, as comments have eluded to, LEFT JOIN is shorthand for LEFT OUTER JOIN, they will produce the same results.
SELECT *
FROM employee
inner join
(
SELECT
employeeid,
count(*) as addresscount
FROM employee
left join employeeaddress ON employeeaddress.employeeaddressid = employee.employeeaddressid
group by employeeid
) counts on counts.employeeid = employee.employeeid
WHERE counts.addresscount = 0 -- Or 1, or 5 or > 1, etc.
LEFT JOIN should be all you need.
SQL Fiddle Example
SELECT e.employeeID ,
e.empName ,
ea.employeeAddressID ,
ea.addr1 ,
ea.addr2
FROM Employee e
LEFT JOIN EmployeeAddress ea ON ea.employeeID = e.employeeID

SQL select, 3 tables

How can I use select if I have 3 tables?
Tables:
school_subject(ID_of_subject, workplace, name)
student(ID_of_student, firstName, surname, adress)
writing(ID_of_action, ID_of_sbuject, ID_of_student)
I want to write the student's name and surname (alphabetically) who have workplace=home.
Is this possible? And how to alphabetize?
SELECT s.firstName, s.surname
FROM student S, school_subject P, writing Z
WHERE P.workplace = 'home'
AND P.ID_of_subject = Z.ID_of_subject
AND Z.ID_of_student = s.ID_of_student;
SELECT s.firstName, s.surname
FROM student S INNER JOIN writing Z
ON Z.ID_of_student = s.ID_of_student
INNER JOIN school_subject P
ON P.ID_of_subject = Z.ID_of_subject
WHERE P.workplace = 'home'
ORDER BY S.firstName, S.surname // Sort the list
To order alphabetically the result it is possible to use ORDER BY keyword. So your query becomes:
SELECT DISTINCT S.firstName, S.surname
FROM student S, school_subject P, writing Z
WHERE P.workplace = 'home' AND
P.ID_of_subject = Z.ID_of_subject AND
Z.ID_of_student = S.ID_of_student
ORDER BY S.surname, S.firstName
The DISTINCT keyword is necessary, because in writing table there are eventually more tuples given keys ID_of_subject and ID_of_student.
So this is necessary to avoid repeating firstName and surname many times.
Note that each student is identified by ID_of_student, not by firstName and surname, so as #danjok said use DISTINCT if you only want the name and surname.
If you want to select all students that satisfy your requirement (even if two or more students have the same firstName and surname), you should including ID_of_student on SELECT clause:
SELECT S.ID_of_student, S.firstName, S.surname
FROM student S
INNER JOIN writing W ON W.ID_of_student = S.ID_of_student
INNER JOIN school_subject P ON P.ID_of_subject = W.ID_of_subject
WHERE P.workplace = 'home'
ORDER BY S.firstName asc, S.surname asc