so ... here we are ... it's not a simple question so first read it carefully please ...
these are my tables
the first image is courses table
the second is instructors table
the third one is teaches table
now i want the names of the instructors who are teaching some courses that have 4 credits
i tried this :
SELECT *
FROM teaches
INNER JOIN course ON ( course.course_id = teaches.course_id
AND course.credits =4 )
but it's not complete yet ... i know which courses have 4 credits but i don't know how to make a relation between this query and the table called instructors
SELECT i.name
FROM instructor I
JOIN teaches T
ON I.ID = T.ID
JOIN courses C
ON C.course_id = T.course_id
WHERE C.credits = 4
Another option in simple way...
select * from instructors
where id in ( select distinct id
from teaches
where course_id in ( select distinct course_id
from courses
where credits = 4
)
);
Related
I am trying to get the remaining tuples from a query. In this case, course_id 3 and 4 from courses because the user admin# has NOT taken these (they've only taken 1 and 2). The tables are already joined nicely and queries are working when I try a LEFT JOIN ...
select course_name from courses LEFT OUTER JOIN users_courses ON users_courses.user_course_id = courses.course_id where users_courses.user_email = "admin#---.com";
But this returns the courses 1 and 2, not 3 and 4
you could do another thing by getting all of the courses that is not exists in the courses list for this user like this
select course_name from courses where course_id not in (select user_course_id from users_courses where user_email = 'admin#....')
update
forget to add the column name course_id
Ah! I misunderstood what you were after. Joseph's method will work, but, as a rule, you should try to avoid "in(select...".
To knock-out the courses the admin has taken, just add a null condition:
select course_name from courses LEFT OUTER JOIN users_courses
ON users_courses.user_course_id = courses.course_id
and users_courses.user_email = "admin#---.com"
where users_courses.course_id is null;
I have three MySQL tables as below:
CREATE TABLE students (
StudentName VARCHAR(255),
StudentID VARCHAR(255)
);
CREATE TABLE courses (
CourseName VARCHAR(255),
CourseID VARCHAR(255)
);
CREATE TABLE participation (
CourseID VARCHAR(255),
StudentID VARCHAR(255)
);
INSERT INTO students
VALUES ('Biplab', '04CS9501'),
('Amit', '05CS3001'),
('Rahul', '05CS3003'),
('Divya', '05CS3004'),
('Praveen', '05CS3005');
INSERT INTO courses
VALUES ('Business and Science', 'B3D'),
('Economics', 'B3B'),
('Business and Laws', '63O1'),
('Economics and Laws', '63K1'),
('Economics and Science', 'B3E');
INSERT INTO participation
VALUES ('B3D', '04CS9501'),
('B3D', '05CS300'),
('B3D', '05CS3003'),
('B3B', '05CS3003'),
('B3B', '05CS3004'),
('63O1', '04CS9501'),
('63O1', '05CS3004'),
('63K1', '05CS3001'),
('63K1', '05CS3003'),
('63K1', '05CS3004');
Working on the assumption that every student must take a course and every course must have student participation, I would like to test the integrity of the data by being able to identify the courses which do not have students or students that do not have course participation. In this case, student "Praveen" and course "Economics and Science".
How would such a query be constructed?
You can use the following solution to get all courses without participation:
-- to get all empty courses using "WHERE NOT ... IN ..."
SELECT *
FROM courses
WHERE NOT CourseID IN (SELECT CourseID FROM participation)
-- or using "LEFT JOIN"
SELECT c.*
FROM courses c LEFT JOIN participation p ON c.CourseID = p.CourseID
WHERE p.StudentID IS NULL
... and the following solution to get the students without participation:
-- to get all students wihtout participation using "WHERE NOT ... IN ..."
SELECT *
FROM students
WHERE NOT StudentID IN (SELECT StudentID FROM participation)
-- or using "LEFT JOIN"
SELECT s.*
FROM students s LEFT JOIN participation p ON s.StudentID = p.StudentID
WHERE p.CourseID IS NULL
You can also use a LEFT JOIN to get the expected records. Additionaly you can use UNION ALL and a additional column to get all records with one query:
SELECT 'Student' AS type, s.StudentID AS 'ID', s.StudentName AS 'Name'
FROM students s LEFT JOIN participation p ON s.StudentID = p.StudentID
WHERE p.CourseID IS NULL
UNION ALL
SELECT 'Course' AS type, c.CourseID, c.CourseName
FROM courses c LEFT JOIN participation p ON c.CourseID = p.CourseID
WHERE p.StudentID IS NULL
ORDER BY type, Name
demo on dbfiddle.uk
You can use a correlated subquery with NOT EXISTS to check for students with no record in the participation table.
SELECT *
FROM students s
WHERE NOT EXISTS (SELECT *
FROM participation p
WHERE p.studentid = s.studentid);
And analogous for the courses.
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"
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