1st table Courses columns:
courseId|instructorId|desc|courseName|img|
2nd table StudentCourses columns:
studentCourseId|studentId
3rd table UserData columns:
userId|avatar|name
So knowing studentId,i am trying to get students owned courses with its instructors profiledata(in this case avatar and name)
result columns should be:
courseId,courseName,desc,img,instructorsname,instructorsavatar
This query gives me students courseInfo but instructor data is not included.
SELECT Courses.courseId,Courses.img,Courses.courseName,Courses.`desc` FROM Courses JOIN StudentCourses ON StudentCourses.studentCourseId = Courses.courseId WHERE StudentCourses.studentId ="igkw11tkwa06kpmoe9o6hyytrq0qaqjq"
assuming that instructorId match with userid
SELECT Courses.courseId
,Courses.img
,Courses.courseName
,Courses.`desc`
, u1.name
, u1.avatar
FROM Courses
INNER JOIN StudentCourses ON StudentCourses.studentCourseId = Courses.courseId
INNER JOIN UserData u1 ON u1.userId = Courses.instructorId
WHERE StudentCourses.studentId ="igkw11tkwa06kpmoe9o6hyytrq0qaqjq"
Related
I have three tables
Student
-------------
id,
matno,
Surname,
faculty_id,
dept_id,
....
Faculty
------------
id,
name,
...
department
-------------------
id,
name,
....
I want to select student details using matno plus the name of faculty and name of department using faculty_id and dept_id . I have tried all SQL JOIN combinations i can think of and I only get one name field.
Also I get null on second query if I try to fetch query using different statement.
Edited
My SQL query is
SELECT * FROM student INNER JOIN faculty ON faculty.id = student.faculty_id LEFT JOIN department ON department.id = student.dept_id WHERE student.matno = 1104
Assuming that faculty_id and dept_id are foreign keys into the Faculty and department tables, this query should give you your desired result (replace ??? with the desired search value of matno):
SELECT s.matno, s.surname, f.name AS faculty, d.name AS department
FROM Student s
JOIN Faculty f ON f.id = s.faculty_id
JOIN department d ON d.id = s.dept_id
WHERE matno = ???
SELECT * FROM
STUDENT S
LEFT JOIN FACULTY F
ON S.FACULTY_ID = F.ID
LEFT JOIN DEPARTMENT D
ON S.DEPT_ID = D.ID WHERE S.MATNO = "$your_matno";
Try using this left join so that at least all students will be in the result set and select the required fields from it.
I'm fairly new to MySQL, and trying to understand the many-to-many relationship since these examples can popup in interviews
There are 3 tables, and since a Student can have many courses and a Course can have many students, this is a Many-to-Many relationship right?
The tables are
Student- has student ID, name, date of birth, and department.
Courses- Has ID, Name of course
Student_Courses- Has student_id, course_id
How would I display these 2 questions-
1) Given a studentID, return all the names of the courses the student is taking
2) Return the name of students who is taking X amount of courses or more (Ex. 4 or more courses).
Im trying to write queries on these, but I'm stuck...
In the case of selecting all of the courses for a given student ID you could try the following, which will return one row for each Course a Student is associated with.
select
s.name as StudentName,
c.name as CourseName
from `Student` as s
inner join `Student_Course` as sc on (sc.student_id = s.ID)
inner join `Course` as c on (c.ID = sc.course_id)
where
(s.`ID` = 'given_Student_ID_here')
;
As for selecting a list of the names of Students taking N or more courses, for this you might use an aggregating sub-select as a WHERE clause in which we reference one of the outer tables (i.e. [Student]) so that the result of the aggregation is personalised per Student record:
select
s.name as StudentName
from `Student` as s
where
(
(
select count(*)
from `Student_Course` as sc
inner join `Course` as c on (c.ID = sc.course_id)
where (sc.student_id = s.ID)
) >= 4
)
;
You might also consider an alternative approach to this second problem by using the GROUP BY and HAVING clauses:
select
s.name as StudentName
from `Student` as s
inner join `Student_Course` as sc on (sc.student_id = s.ID)
inner join `Course` as c on (c.ID = sc.course_id)
group by
s.name
having
count(*) >= 4
;
Table instructor: ID, name, dept_name, salary
Table student: ID, name, dept_name, tot_cred
Table advisor: s_ID, i_ID which contains student id and instructor id for the two table.
I need to find all the instructor and student's name where the department of the advisor is CComp.Sci
I can find all the id of the instructor and student whrer the intructor is fom Computer science. And only the name of the students.
But can't figure out both the name at the same time.
I wrote this:
SELECT student.name
FROM student
WHERE student.ID in (SELECT advisor.s_ID
FROM advisor
, instructor
WHERE advisor.i_ID = instructor.ID
and instructor.dept_name = 'Comp.Sci')
The root solution I think you need here is just a simple join between the three tables. But since you need a single list of both student and instructor names, this complicates things. One option is to union together a query which finds the matching students along with a query which finds the matching instructors.
SELECT s.name, 'student' AS type
FROM student s
INNER JOIN advisor a
ON s.ID = a.s_ID
INNER JOIN instructor i
ON a.i_ID = i.ID
WHERE i.dept_name = 'CComp.Sci'
UNION ALL
SELECT DISTINCT i.name, 'instructor'
FROM student s
INNER JOIN advisor a
ON s.ID = a.s_ID
INNER JOIN instructor i
ON a.i_ID = i.ID
WHERE i.dept_name = 'CComp.Sci'
Using "where .. in (..)" will give you bad performance, also it will not allow you to get the data from the table in the where clause.
Here is a solution if you mean to get the result of instructor name side alone with student name.
SELECT S.name AS Student,I.name AS Instructor
FROM Students AS S
JOIN Advisor AS A ON A.s_ID = S.Id
JOIN Instructor AS I ON I.Id = A.i_ID
WHERE I.dept_name = 'Comp.Sci'
*notice that I used alias
** if you want for example to get all students even those who lack instructor use LEFT JOIN
Suppose that we have four tables in the following structure (Table name - fields):
person - id, name
doctor - id_person, specialty
pacient - id_person, disease
appointment - doctor_id, pacient_id, date
How can I construct a query to return the doctor's name and specialty, the pacient's name and disease and the appointment date?
Here's where I've got so far:
SELECT person.name, doctor.specialty, pacient.disease, appointment.date
FROM appointment
INNER JOIN person
ON appointment.pacient_id=person.id
INNER JOIN doctor
ON appointment.doctor_id=doctor.id_person
INNER JOIN pacient
ON appointment.pacient_id=pacient.id_person
But this is not returning the right fields. I think the problem resides in returning the same field (person.name) for two different ids (doctor and pacient) in the same row.
You need to do two separate joins to the person table, and use aliases to identify the individual tables like so:
select
dp.name as DoctorName
, doctor.specialty
, pp.name as PacientName
, pacient.disease
, appointment.date
from appointment
inner join doctor
on appointment.doctor_id = doctor.id_person
inner join person dp
on appointment.doctor_id = dp.id
inner join pacient
on appointment.pacient_id = pacient.id_person
inner join person pp
on appointment.pacient_id = pp.id
I have 4 tables; loans (has 'user_id'), users (has 'student_ID' and 'teacher_ID'), student, and teacher.
The code I tried:
SELECT *FROM loans LEFT JOIN users ON loans.user_id=users.user_id
(
IF student_ID='1' THEN (SELECT *FROM teacher)
ELSE (SELECT *FROM student)
)
The code is not working properly. I want the loans display the student or teacher name from users. I have tried similia work, and it doesn't work either.
Try this:
select *
from loans l
left join users u on l.user_id = users.user_id
left join teacher t on u.teacher_id = t.teacher_id and u.student_id = 1
left join student s on u.student_id = s.student_id and u.student_id <> 1
Explanation
Extract every record in loans
Extract records in users where there's a match on user_id
Extract records from teacher matching on teacher_id if user's student_id is 1
Extract records from student matching on student_id if user's student_id is NOT 1