How to make SQL query inner join when tables are connected by a bridge table? - sql

I have create many to many relationships using code first entity framework.
Here the tables generated in DB:
I want to make sql query inner join Students on the Courses,but I don't know how to implement it in this case when bridge table between them.
How can I make sql query inner join Students on the Courses?

Try this
Select c.CourseId,c.CourseName,sc.StudentId,s.StudentName
from Courses c Inner Join StudentCourses sc On c.CourseId = sc.Course_CourseId
Inner Join Student s ON sc.Student_StudentId = s.StudentId

Related

SQL Inner join many to many multi part identifier not bound

select Course.CourseName as course_coursename
,Instructor.Name as instructor_instructorname
from Course
inner join CourseInstructor as CI
on Instructor.InstructorID=CourseInstructor.InstructorID
and Course.CourseID = CourseInstructor.CourseID
This is a query I made to inner join many to many table. I have course and instructor table and join table which includes the course and instructorid.
I want to inner join the tables to display the courses taught by each instructor but getting an error:
multipart identifier could not be bound
.. for instructor.name, instructor.insturctorid, courseinstructor.instructorid and courseinstructor.courseid.
The error basically states that you are referencing a table called Instructor that the query cannot find in the query itself. Your SELLECT does reference a table called Instructor, but the CourseInstructor table is subsequently aliased as CI:
inner join CourseInstructor as CI
Change that to
inner join CourseInstructor as Instructor
you need to use everywhere in the SELECT and in the ON clause the same table names or aliases that are defined in the FROM clause
So your query mus look like this
select Course.CourseName as course_coursename
,Instructor.Name as
instructor_instructorname
from Course inner join
CourseInstructor as Instructor on Course.InstructorID = Instructor.InstructorID
AND Course.CourseID = Instructor.CourseID

sql join tables IS NOT

I need to get the data of all teachers who gives a course but does not have an assignement. I tried using inner or left join so if the name of the teacher is not in the table of the assignments it will show the data. If the name of the teacher is in the table of assignments it must not show the data. But I could not get it working.
What am I doing wrong?
select teachers.nameteacher, courses.namecourse, courses.codecourse
from teachers
inner join courses
on teachers.codecourse = courses.codecourse
left join assignments
on assignments.nameteacher = teachers.nameteacher
where teachers.nameteacher IS NULL
filter should be from assignments table
SELECT t.nameteacher,
c.namecourse,
c.codecourse
FROM teachers t
INNER JOIN courses c
ON t.codecourse = c.codecourse
LEFT JOIN assignments a
ON a.nameteacher = t.nameteacher
WHERE a.nameteacher IS NULL --should be assignments
Note : start using alias names to make the query more readable
The easy (and efficient) way, if you are using PostgreSQL or Microsoft SQL Server is to say:
select
a.nameteacher,
c.namecourse,
c.codecourse
from
(
select nameteacher from teachers
except
select nameteacher from assignments
) a
join teachers b
on a.nameteacher = b.nameteacher
join courses c
on b.codecourse = c.codecourse;
If you are using Oracle change "except" to "minus".
If you are using SQLite or MySQL you need to use one of the other suggestions because these don't support except or minus.

How can i apply left outer join conditions on four tables?

i was trying to apply joins on 4 tables. but i could not find proper result for that.
i have 4 tables like 1.students,2.college,3.locations,4.departments. so i have same column sid in all tables which can be used to join conditions.
i want all matched rows from four tables as mentioned columns in select statement below and unmatched rows in left table which is left outer join work.
i have tried this syntax.
select
students.sname,
college.cname,
locations.loc,
department.dept
from students, college, locaions, departments
where student.sid=college.sid(+)
and college.sid=locations.sid(+)
and locations.sid=department.sid(+);
is this right ?
This is an old-fashioned way of outer-joining in an Oracle database. For Oracle this statement is correct; in other DBMS it is invalid.
Anyway, nowadays (as of Oracle 9i; in other DBMS much longer) you should use standard SQL joins instead.
select
s.sname,
c.cname,
l.loc,
d.dept
from students s
left outer join college c on c.sid = s.sid
left outer join locations l on l.sid = c.sid
left outer join departments d on d.sid = l.sid;
Given what you've shown it would appear that what you really want is
select s.sname,
c.cname,
l.loc,
d.dept
from students s
LEFT OUTER JOIN college c
ON c.SID = s.SID
LEFT OUTER JOIN locations l
ON l.SID = s.SID
LEFT OUTER JOIN departments d
ON d.SID = s.SID
The issue in your original query is that because an OUTER JOIN is an optional join, you can end up with NULL values being returned in one of the join fields, which then prevents any of the downstream values being joined. I agree with #ThorstenKettner who observes in a comment that SID is apparently a "student ID", but it's not reasonable or appropriate to have a "student ID" field on tables named COLLEGE, LOCATIONS, or DEPARTMENTS. Perhaps you need to update your design to allow any number of students to be associated with one of these entities, perhaps using a "join" table.
Best of luck.

SQL statement - difficulty producing two fields from one table

I am relatively new to SQL and able to write some simple statements with some JOIN, WHERE thrown in. I also have some experience with SSRS 2008. However I am having difficulty producing two (unique) lists of names based on one table.
I am trying to produce a report containing Staff Members which lists Clients they look after. All names (regardless whether Staff or Client) are held in the Person table. I can run a simple query listing all staff members or clients but I am unable to list both.
To produce the Client list my query would be simply
SELECT p.Forenames, p.Surname
FROM Person AS p
INNER JOIN Client AS c ON p.ID = c.ID
and to produce the Staff list my query would be as above but the INNER JOIN as follows:
INNER JOIN StaffMember AS s ON p.ID = s.ID
The link between Staff Member and Client with all the different links are as follows (for reference).
Client.ID = ClientRecordForm.Client_ID
Person.ID = ClientRecordForm.AssignedSupportWorker_ID
Person.ID = StaffMember.ID
StaffMember.ID = ClientRecordForm.AssignedSupportWorker_ID
To help illustrate this, I can run a query to list Staff Members who have been assigned to Clients.
SELECT DISTINCT p.Forenames, p.Surname
FROM Person AS p
INNER JOIN ClientRecordForm AS crf ON p.ID = crf.AssignedSupportWorker_ID
This last query is essentially what I want but I am struggling to add the Client names as I don't seem to be able to distinguish the Clients as I am already using the Person table
If you want to show persons and be able to distinguish clients as well, try a self join.
SELECT DISTINCT p.Forenames, p.Surname
FROM Person AS p
INNER JOIN ClientRecordForm AS crf ON p.ID = crf.AssignedSupportWorker_ID
inner join person as personClients on crf.clientid = personClients.id
As you can see, you could then join another persons table to get StaffMember, you can join from personClients to Client to get information there ...etc.
Is that what you want?
SELECT DISTINCT p.Forenames, p.Surname
FROM Person AS p
INNER JOIN ClientRecordForm AS crf
ON p.ID = crf.AssignedSupportWorker_ID
INNER JOIN Client AS c
ON c.ID = crf.Client_ID

SQL Query - Join

I have a Table which contains student information(name, id, course,....), and I have another table which contains information like(id, student leave)
I need to generate a report, which is by course all the students who were absent....
Am confused on how to do this properly - Do i use an outer join or what cause SQL 5.0 keeps giving me errors like syntax error....
Have to use this query in servlets...!!!
Currently am using the query in 2 parts.... but which is not generating the table properly....
SELECT * FROM Student s LEFT JOIN Attendance a ON a.student_id = s.id WHERE s.course_id = "ENG"
SELECT *
FROM Student s, Attendance a
WHERE s.id = a.student_id
AND s.course_id = "ENG"
AND s.id = <the_retrieved_id>
Should also be possible.
I suggest turning Student.id to Student.student_id to make it clear,
and also use integer for keys in course table.
then use join like Pradeep Singh only with "using":
select * from Student s left join Attendance a using (student_id) where s.course_id="ENG"
Select * From Student s left join Attendance a on a.student_id = s.id where course_id='ENG'