Filtering joins with conditions - sql

I have 3 table, named as
students (id, name)
subjects (id, name, is_published)
student_subjects (id, student_id, subject_id)
The subjects taken by a student goes in student_subjects. But there are cases where student_subjects contains NULL subject_id for a student
Here's the data for Students table
1;"John"
2;"Ahmeah"
3;"Dina"
4;"Leo"
5;"Lenon"
Subjects Table
1;"Computer Sci";1
2;"Physics";1
3;"Bio";1
4;"Maths";0
Student_subjects
1;1;1
2;1;2
3;1;4
4;2;1
5;2;3
6;2;4
7;3;2
8;4;1
9;5;NULL
Currently to fetch all the students with their subjects and also display the name of the student who has no subject attached to him, I am using the query as follows
SELECT
students.*,
inners.name
FROM
students
LEFT JOIN ( SELECT
student_id,
subjects.name
FROM
student_subjects
JOIN subjects ON ( student_subjects.subject_id = subjects.id AND subjects.is_published = 1)
) AS inners ON (inners.student_id = students.id )
Is there a better way to do the same http://sqlfiddle.com/#!12/9cf93/12

SELECT s.*, su.name AS SubjName
FROM students AS s
LEFT JOIN student_subjects AS ss ON ss.student_id = s.id
LEFT JOIN subjects AS su ON su.id = ss.subject_id
Does the same thing as the one you listed. Is that what you are after?

Related

SQL select - Get all from exams table where examId exists in subjects table, for all subjects that exist in students table for desired studentId

I have three tables: exams, subjects and students.
Students table has foreign key subjectId and Subjects table has foreign key examId.
I need to retrieve all exams for desired studentId. So I need sql select that will pick up all subjects for that studentId, and pick up all exams for all these subjects.
So pseudo code is something like:
SELECT * FROM exams
WHERE id IN (SELECT examId FROM subjects
WHERE id IN (SELECT subjectId from students
where id === desiredId))
Why are you using subqueries and just not inner join?
Try with this:
Select X.* From exams X
Inner join subjects S on S.examId = X.id
Inner join students T on T.subjectId = s.Id
Where T.id =#yourStudentId

'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 query with more than 2 tables

I'm doing an exercise on ORACLE SQL.
Currently I got 3 tables.
Student values = "student_id ,name"
Subjects values = "subject_id, name"
Scores values = "score, student_id, subject_id"
I'm trying to retrieve the following information from my database.
Name of student, Name and id of the subject and finally the score that has the student_id "34560".
SELECT scores.score,
scores.subject_id,
student.name,
subject.subject_id,
subject.name
FROM scores
INNER JOIN students
ON scores.student_id = '34560'
INNER JOIN subject
ON /* and here's where i'm lost*/
Is there a way to put all together from the first part of the query where I call the list of students with student_id = "34560" and then query that list to see if it matches with the subject_id?
Use in operator for list of student id
SELECT sc.score, sc.subject_id,
st.name, sb.subject_id, sb.name
FROM scores sc
INNER JOIN students st
ON sc.student_id = st.student_id
INNER JOIN subject sb
ON sc.subject_id=sb.subject_id
where sc.student_id in ('34560','add_anotherstudentid','add_anotherstudentid') //you can add multiple student id

select sections which do not have any students enrolled

I want to select all sections which do not have any students enrolled
These are the three tables:
ENROLLMENTS
student_id, section_id
SECTIONS
course_id, section_id
COURSES
course_id, description
The output table should look like this:
course_id | description | section_id
I'm not shure which join to use.
So you want
SELECT
course_id
, description
, section_id
FROM
COURSES
INNER JOIN
SECTIONS
ON
COURSES.course_id = SECTIONS.course_id
LEFT JOIN
ENROLLMENTS
ON
ENROLLMENTS.section_id = SECTIONS.section_id
WHERE
ENROLLMENTS.student_id IS NULL;
This will take all of the information from COURSES and SECTIONS, then join it to ENROLLMENTS, keeping all of the information for COURSES and SECTIONS where there an no enrollments. Then by selecting where the ENROLLMENTS student_id is NULL we find all the entries that didn't have section_ids in SECTIONS.
Why not using a subquery?
SELECT
sections.course_id,
courses.description,
sections.section_id
FROM
courses INNER JOIN sections ON courses.course_id = sections.course_id
WHERE
courses.section_id NOT IN (SELECT section_id FROM enrollments)

Multiple joins onto same table

i have the following tables:
TABLE: teachers:
teacherID
teacherName
TABLE: students:
studentID
studentName
teacherID
advisorID
so, usually, i know i can get a single row per student, with their teachers name using an INNER JOIN.
but in this case - the advisor and tacher - are from the same teachers table. so how can i join onto the teachers table twice - once getting the teacher name, and then again to get the advisor name?
hope this is clear
thanks!
This lists students with the names of their teachers and advisors if any, in alpha order of student, without either (a) the teacher or (b) the advisor having to exist. If you want only where those names exist, change the respective join to an INNER join.
SELECT s.studentname as [Student], t.teachername as [Teacher], a.teachername as [Advisor]
FROM Students s
LEFT JOIN Teachers t ON s.TeacherID = t.TeacherID
LEFT JOIN Teachers a ON s.AdvisorID = a.TeacherID
ORDER BY 1, 2
You can join to the same table more than once, just give it a different alias for each join, and name your fields in a descriptive enough way. Use a left join if there might not be a link, but if a student always has both a teacher and an advisor, a straight join should be fine.
Something like this:
select s.studentname student
, t.teachername teacher
, a.teachername advisor
from students s
join teacher t
on t.teacherID = s.teacherID
join teacher a
on a.teacherID = s.teacherID
Why not try something like the following. Its been a while since I've done SQL so this may not work.
SELECT s.studentName AS Student, t.teacherName AS Teacher, a.teacherName AS Advisor
FROM teachers t, teachers a, students s
WHERE t.teacherID = s.teacherID AND a.teacherID = s.advisorID