Multiple joins onto same table - sql

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

Related

Getting data from unrelated tables

I've got this homework for database 'SCHOOL'
Table Student (StudentID, Name, Surname, etc..)\
Table Application (ApplicationID, StudentID, ClassID)\
Table Class (ClassID, ClassName, TeacherID)\
Table Teacher (TeacherID, Name, Surname, etc..)
There are some other tables and columns in the database but I don't think they're important to this query.
I need to get name of the teachers for whose classes no student signed up. Let's say there is no one signed up for Math class, and I need to get the name of the teacher for that Math class.
SELECT Teacher.Name, Teacher.Surname, Class.ClassName
FROM Teacher
INNER JOIN Class ON Class.TeacherID = Teacher.TeacherID
INNER JOIN Application ON Application.ClassID = Class.ClassID
INNER JOIN Student ON Student.StudentID = Application.StudentID
WHERE Application.PredmetID IS NULL
Help would be appreciated.
I would recommend NOT EXISTS:
select t.name
from class c join
teacher t
on t.teacherid = c.teacherid
where not exists (select 1
from application a
where a.classid = c.classid
);
I need to get name of the teachers for whose classes no student signed up
Select T.name
From Teacher T inner join [Class] C on T.TeacherId = C.TeacherId
Where C.ClassId not in (Select Distinct ClassId from Application)

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

Difficulty understanding a SQL Join query

I have these tables in my database:
Student:
id,
style_id,
instrument,
name,
Teacher:
id,
style_id,
name
lessons:
id,
style_id,
teacher_id,
student_id,
month
And I'm trying to write a query that returns every student enrolled for a particular month and their teacher and style e.g. it would return something like this:
March
Student Name Teacher Style
-------------------------------
Glen Bob Jazz
Missy Bruce Rock
So far all I got is something like this (I know I need to use joins just don't know how):
SELECT
students.name, teacher.name, style.style_name
FROM
lessons
WHERE
month = "March"
JOINS students, teachers
First off, it is not a good practice for you to have the name of the table in your primary key, in your table teacher, you used musician_id, use teacher_id instead. This is the code I would use to achieve what you are trying to do.
SELECT a.style_name, b.name, c.name, d.month
FROM lessons d
INNER JOIN Styles a ON a.style_id = d.style_id
INNER JOIN Student b ON b.student_id = d.student_id
INNER JOIN Teacher c ON c.teacher_id = d.teacher_id
WHERE d.month = "march";
Since you are in difficulty with joins, Inner join, will see if its evidence of the ids in both tables to return true, thats why it solvs your problem.
To answer your specific question,
SELECT Student.name AS Student, Teacher.name AS Teacher, Styles.style_name AS Style
FROM lessons
INNER JOIN Styles ON lessons.style_id = Styles.style_id
INNER JOIN Teacher ON lessons.teacher_id = Teacher.musician_id
INNER JOIN Student ON lessons.student_id = Student.student_id
WHERE lessons.month="March";
You might want to look at your table design, though. You have a style_id in the lessons, the Teachers and the Students tables. The query above matches the style name with the style_id in the lessons table, while it could be linked to either the Teacher table or the Student table. This could lead to reporting problems if Bob is teaching a Jazz lesson and his profile says he's a Rock teacher.
It's better to remove the style_id from the Teacher and Student tables and introduce a new table
Interests: participant_id (could be teacher id or student id), role ('teacher' or 'student'), style_id

Filtering joins with conditions

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?

what should be the relation ship between both the tables so each student can have separate marksid

i am new to sql
I have 2 tables first one with marks and second one with students.
marks have a primary key of marksid and students have primary key student id.
I want to know what should be the relation ship between both the tables so each student can have separate marksid.
and what will be the sql query to get their data.
I have tried one to one relationship with this select query
s="select s.Student Name,z.Hourly1Marks from Student s,marks z "
but it is showing Cartesian product of entries.
Study the concept of INNER JOIN (if each student has one mark, i.e. 1:1 relationship), and LEFT JOIN (if each student can have multiple marks, i.e. 1:n relationship).
You used an inner join without a restriction. What you need is something like
SELECT S.name, Z.marks
FROM students S
JOIN marks Z ON S.student_id = Z.student_id
or equivalently
SELECT S.name, Z.marks
FROM students S, marks Z
WHERE S.student_id = Z.student_id
for a 1:1 relationship case, or
SELECT S.name, Z.marks
FROM students S
LEFT JOIN marks Z ON S.student_id = Z.student_id
for a 1:N relationship case.
Supposing you have two tables:
Students
--------------
IDStudent
Name
....
Marks
--------------
IDMarks
Hourly1Marks
and the relation between Studend and Marks is 'A student could have 0 or more Marks' then you need to add a column in the table Marks - call it studentID and this column is used as foreign key for the student table.
then you can write this query
select s.Name, z.Hourly1Marks
from Student s left join marks z
on z.studentID = s.IDStudent
that will give you back all students and their marks (also the student without any marks)
or, if you want only the students with at least one mark
select s.Name, z.Hourly1Marks
from Student s inner join marks z
on z.studentID = s.IDStudent