MySql Query : How to Write Inner Select Join Query - mysql

I have five tables: 1. courses 2.departmetns 3.students 4. enroll_courses 5.results
Departments has id
Courses has id, department_id
Students has id,department_id
Enroll_courses has id, courses_id,students_id
Results has id, courses_id,students_id
I have to select courses.name which are in enroll_courses tables but not in results table.
I have written the query, I couldn't get the exact answer. If anyone can help me to find the solution.
Query:
SELECT courses.name
FROM courses
JOIN departments ON courses.department_id = departments.id
JOIN students ON departments.id = students.department_id
WHERE students.id = 9
AND courses.id IN (SELECT course_id FROM enroll_courses)

Its a little bit blind shoot since you didn't provide any DDL's , but I assume results table have a course_id column , if not, change it to the relation. You can do this with EXISTS() and NOT EXISTS():
SELECT courses.name
FROM courses
JOIN departments ON courses.department_id = departments.id
JOIN students ON departments.id = students.department_id
WHERE EXISTS(SELECT 1 FROM enroll_courses ec WHERE ec.course_id = courses.id)
AND NOT EXISTS(SELECT 1 FROM results r WHERE r.course_id = courses.id)
AND students.id =9

Related

get all records from database that meet my needs with raw SQL query

I have courses table and teachers table in MySQL database.
I would like to find all new courses that are not yet assigned to any recruited teacher of this year that age is younger than 30.
The relationship is each course has many teachers. So, in teachers table, there is course_id column
teachers table has a year column to indicate which year the teacher is recruited and an age column.
The SQL I tried:
SELECT courses.* from courses
INNER JOIN teachers t ON t.course_id = courses.id
WHERE t.year = '2019' AND t.age<30
This gives actually all courses that have been assigned to teachers recruited this year and age is younger than 30, which is opposite than what I need.
I would like to filter the above by combine with the following query:
SELECT COUNT(*) from teachers WHERE year=2019 that returns zero.
I am not sure how to do so. Could someone please help me?
Finishing the Zaynul's answer:
select courses.*
from courses
left join teachers t on t.course_id = courses.id and t.year = '2019' and t.age<30
where t.course_id is null
See SQLFiddle
To further comment on the data structure, the year should be an integer and the course-teacher relation should be in it's own table, otherwise a teacher can only teach one course.
This query:
select course_id
from teachers
where year = 2019 and age < 30
returns all the course_ids that you don't want in your results.
You can do a left join of courses to this query and get only the non matching rows:
select c.*
from courses c left join (
select course_id
from teachers
where year = 2019 and age < 30
) t on t.course_id = c.id
where t.course_id is null
or use NOT IN:
select * from courses
where id not in (
select course_id
from teachers
where year = 2019 and age < 30
)
Another way with NOT EXISTS:
select c.* from courses c
where not exists (
select 1 from teachers t
where t.course_id = c.id and t.year = 2019 and t.age < 30
)
try using left join
SELECT courses.* from courses
left JOIN teachers t ON t.course_id = courses.id
WHERE t.year = '2019' AND t.age<30
and t.course_id is null

How can I do this relational division query?

I want to do a relational division query. First, here is the structure of each table:
Student
id
name
Course
id
name
Student_passed_course (junction table that stores who passed which course)
id_student
id_course
Basically, what I want is to get the names of the students that have passed all the courses that exist in table Course using JOIN (or LEFT JOIN, etc). I already implemented a solution using NOT EXISTS.
Also this is the equation I made.
A solution based on JOIN could be as follow:
SELECT s.name AS student_name, c.name as course_name FROM (
SELECT s_id, c_id, SUM(c_exists) AS c_exists, SUM(c_taken) as c_taken, SUM(c_exists)-SUM(c_taken) as not_taken FROM (
SELECT s.id as s_id, c.id as c_id, 1 as c_taken, 0 as c_exists FROM student s JOIN student_passed_course spc ON spc.id_student = s.id JOIN course c ON c.id = spc.id_course
UNION ALL
SELECT s.id as s_id, c.id as c_id, 0 as c_taken, 1 as c_exists FROM student s JOIN course c
) X group BY s_id, c_id) Y
JOIN student s ON s.id = s_id JOIN course c ON c_id = c.id
WHERE not_taken = 1;
I don't know if this would be more or less efficient than your solution.

How do I query a set that doesn't contain any members from another set in MySQL?

I'd like to return a list of students who have taken classes in one department but not in another. Here's my query and its result is coming up blank for some reason.
SELECT *
FROM student
JOIN transcript
ON student.id = transcript.studID
JOIN course
ON transcript.crsCode = course.crsCode
WHERE deptId = "CSCI" NOT IN
(
SELECT student.name
FROM student
JOIN transcript
ON student.id = transcript.studID
JOIN course
ON transcript.crsCode = course.crsCode
WHERE deptId = "MATH"
);
Here are what the tables look like:
Student (id, name, address, status)
Transcript (studId, crsCode, semester, grade)
Courses (crsCode, deptId, crsName, descr)
Without using sub queries:-
SELECT DISTINCT student.*
FROM student
JOIN transcript
ON student.id = transcript.studID
INNER JOIN course c1
ON transcript.crsCode = c1.crsCode
AND c1.deptId = 'CSCI'
LEFT OUTER JOIN course c2
ON transcript.crsCode = c2.crsCode
AND c2.deptId = 'MATH'
WHERE c2.crsCode IS NULL
This is joining student against transcript. It then joins against course twice, once for the course you want and a LEFT OUTER JOIN for the course you don't want. The WHERE clause checks that there was no match on the course you do not want.
The DISTINCT is used to limit the results to single occurrences. This may not be necessary, but that depends on whether a single student can have done courses multiple times.
You could use two exists conditions - one for the department you want to include and one for the department you want to exclude.
SELECT s.*
FROM student s
WHERE EXISTS (SELECT *
FROM transcript t
JOIN courses c ON t.crsCode = c.crsCode
WHERE deptId = 'MATH' AND t.studId = s.id) AND
NOT EXISTS (SELECT *
FROM transcript t
JOIN courses c ON t.crsCode = c.crsCode
WHERE deptId = 'CSCI' AND t.studId = s.id)
To check two conditions you need to add AND clause in your query and check student.name is NOT IN in your sub-query as:
SELECT *
FROM student
JOIN transcript
ON student.id = transcript.studID
JOIN course
ON transcript.crsCode = course.crsCode
WHERE deptId = "CSCI" AND student.name NOT IN
(
SELECT student.name
FROM student
JOIN transcript
ON student.id = transcript.studID
JOIN course
ON transcript.crsCode = course.crsCode
WHERE deptId = "MATH"
);

SQL query to find the students in one course

There are three tables
Students, Courses and Registration
Students has id, name columns
Courses has also course.id, course.name
and there is third table joining the Students and Courses table
Registration : stu_id, course_id
One Student can take one or many courses.
I would like to find the name of Students registered in only one course.
Try with INNER JOIN
SELECT S.id, S.name
FROM students S
INNER JOIN registration R ON S.id = R.stu_id
GROUP BY S.id, S.name
HAVING COUNT(*) = 1
Like below:
SELECT s.id, s.name
FROM students s
LEFT JOIN registration r ON s.id = r.stu_id
GROUP BY s.id, s.name
HAVING COUNT(r.course_id) = 1
select s.*
from (
select r.stu_id stu_id
from Registration r
group by r.stu_id
having count(*) == 1) ra
join Students s on s.id = ra.stu_id;
This one is more efficient.
It's unlikely that your schema has null fields. Therefore, it doesn't matter which kind of join, inner or left, you use.

How to get data from 4 tables in 1 sql query?

I have the following database schema:
table courses:
id
tutor_id
title
table course_categories:
id
category_id
course_id
table categories:
id
name
table tutors:
id
name
table subscribers:
id
course_id
user_id
I need to make 1 sql to get a course with all it's categories, and the tutor for that course and the number of subscribers for that course. Can this be done in 1 query? Should this be done using stored procedures?
With this query you get what you want:
select co.title as course,
ca.name as category,
t.name as tutor,
count(s.*) as total_subscribers
from courses co
inner join course_categories cc on c.id = cc.course_id
inner join categories ca on cc.category_id = ca.id
inner join tutors t on co.tutor_id = t.tutor_id
left join subscribers s on co.id = s.course_id
where co.title = 'Cat1'
group by co.title, ca.name, t.name
I used left join on subscribers because there might be no one for a given course. I'm assuming that all the other tables have data on it for every course, categorie and tutor. If not, you can user left join as well but then you'll have data with null.
It can be done. You need to look up select and the use of join. See select and join to help complete the assignment
select cou.title, cat.name, tu.name, count(sub.user_id) from courses cou, course_categories cca, categories cat, tutors tu, subscribers sub where cou.id = cca.id and cat.id = tu.id and tu.id = sub.id group by cou.title, tu.name;