SQL query to find the students in one course - mysql

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.

Related

Get attempts of exams for every course of every student

I'm new to SQL. I want to get attempts of exams for every course of every student.
I've made this query in my database:
SELECT students.id, students.surname, students.name, courses.name AS "course" FROM `students`
JOIN exam_student
ON exam_student.student_id = students.id
JOIN exams
ON exam_student.exam_id = exams.id
JOIN courses
ON exams.course_id = courses.id
ORDER BY students.surname ASC, students.name ASC;
The query produces this
Now, what I want is to get for every user, the attempts of exams of every course name.
I have the exams pivot with user_id and course_id.
How can I reach this result?
Seems you want a total.
So let's COUNT the attempts by aggregating it.
SELECT s.id, s.surname, s.name
, c.name AS course
, COUNT(e.id) AS exam_attempts
FROM students s
LEFT JOIN exam_student es ON es.student_id = s.id
LEFT JOIN exams e ON e.id = es.exam_id
LEFT JOIN courses c ON c.id = e.course_id
GROUP BY s.id, s.surname, s.name, c.id, c.name
ORDER BY s.surname, s.name, c.name;

MySql Query : How to Write Inner Select Join Query

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

How to write a query with count

I have two tables as follows:
==================
StudentsClasses
----------------
ID (Registration ID of the class)
StudentID (ID of student taking class)
ClassID (ID of certain class)
----------------
==================
Students
---------------
ID (ID of student)
Name (Name of student)
GradeLevelID (Grade of student)
---------------
==================
And they are joined by StudentsClasses.StudentID and Students.ID.
I am trying to write a query to return the students with the least classes registered. My query is:
SELECT Students.Name, COUNT(StudentsClasses.StudentID) AS Expr1
FROM StudentsClasses INNER JOIN
Students ON StudentsClasses.StudentID = Students.ID
GROUP BY StudentsClasses.StudentID, Students.Name
ORDER BY Expr1
However, that only returns all the students with at least 1 class in ASC order.
I know the correct answer is 7 students with 0 classes.
How can I modify my query to return only those 7 students with 0 classes.
To enlist those students, that have no classes, instead of INNER JOIN you should be using LEFT JOIN here, to make sure all rows from students table are listed, even though there is no rows in studentclasses for that particular student.
SELECT
s.name, count(sc.id) AS classes
FROM
students s
LEFT JOIN studentsclasses sc ON s.id = sc.studentid
GROUP BY s.name
HAVING count(sc.id) = 0 -- added after comment
ORDER BY count(sc.id);
OR another method (for retrieving only students that have 0 classes):
SELECT
s.name
FROM
students.s
LEFT JOIN studentsclasses sc ON s.id = sc.studentid
WHERE
sc.id IS NULL
This should limit your results to those students having whatever the minimum number of registered classes is (so if the minimum is currently zero, then zero, if it becomes true that the least number of registrations is 3, then it will use 3, etc.)
select s.name,
v.classes
from students s
join (
select s.name,
count(sc.id) as classes
from students s
left join studentsclasses sc
on s.id = sc.studentid
group by s.name
order by count(sc.id)
limit 1
) v
on s.name = v.name
Answers for this question.
How can I modify my query to return only those 7 students with 0 classes.
SELECT
s.name, count(sc.id) AS classes
FROM
students s
LEFT JOIN studentsclasses sc ON s.id = sc.studentid
where (SELECT count(sc.id) FROM
students s
LEFT JOIN studentsclasses sc ON s.id = sc.studentid)=0
GROUP BY s.name
ORDER BY count(sc.id);

Is this the right way to join tables to fetch data?

I have a database with the tables:
Student(SID,Name,Surname,Age)
Registration(StudentID,CourseID)
Course(CID,Name,Cost)
I would like to extract only the name of the courses with students younger than 20. Will the query below do just that?
SELECT C.NAME
FROM Course C
INNER JOIN Registration
INNER JOIN Student S
WHERE CID = CourseID
AND SID = StudentID
AND Age < 20
GROUP BY C.NAME
I would also like to extract the number of students in each course having students younger than 20. Is it correct to do it as below?
SELECT count(S.NAME)
,C.NAME
FROM Student S
INNER JOIN Course C
INNER JOIN Registration
WHERE Age < 20
AND CID = CourseID
AND SID = StudentID
GROUP BY C.NAME
You are missing the ON part for the join otherwise it would just be a CROSS JOIN.
Your first query should look like this if you want just a distinct list of student names:
SELECT DISTINCT C.NAME
FROM Course C
INNER JOIN Registration R ON C.CID = R.CourseID
INNER JOIN Student S ON R.StudentID = S.SID
WHERE Age < 20
Your second query shouldn't really have the C.Name in the select if you want to get just a count unless you want a count of how many students have that name.
SELECT count(*)
FROM Student S
INNER JOIN Registration R ON s.SID = R.StudentID
INNER JOIN Course C ON c.CID = R.CourseID
WHERE Age < 20
GROUP BY C.NAME
First join these tables, then group by Course's PK(CID), Add the HAVING condition to filter the course which has students younger than 20.
Then use Course table to join the result to get the course name and count of students in the course.
SELECT
T1.Name,
T2.StudentCount
FROM
Course T1
INNER JOIN (
SELECT
c.CID,
COUNT(s.SID) AS StudentCount
FROM
Course c
LEFT JOIN Registration r ON c.CID = r.CourseID
LEFT JOIN Student s ON s.SID = r.StudentID
GROUP BY c.CID
HAVING COUNT(IF(s.Age < 20, 1, NULL)) > 0
) T2 ON T1.CID = T2.CID
More correctly, you should move the conditions of the join, to the join statements themselves by including them in the on clause instead of the where. While the results may not change in this instance, if you were to start including outer joins you would encounter difficulties.
SELECT count(S.NAME)
,C.NAME
FROM Student S
INNER JOIN Registration R
ON s.SID = R.StudentID
INNER JOIN Course C
ON c.CID = R.CourseID
WHERE Age < 20
GROUP BY C.NAME
There's a fiddle here showing it in action: http://sqlfiddle.com/#!9/c3b8f/1
Your first query will also produce the results you want, but again, you should move the join predicates to the join itself. Also, you don't need to perform the grouping just to get distinct values, mysql has an expression for that called distinct. So rewritten, the first query would look like:
SELECT DISTINCT C.NAME
FROM Student S
INNER JOIN Registration R
ON s.SID = R.StudentID
INNER JOIN Course C
ON c.CID = R.CourseID
WHERE Age < 20.
Again, the results are the same as what you have already but it is easier to 'read' and will put you in good stead when you move on to other queries. As it stands you have mixed implicit and explicit join syntax.
This fiddle demonstrates both queries: http://sqlfiddle.com/#!9/c3b8f/4
edit
I may have misinterpreted your original question - if you want the total number of students enrolled in a course with at least one student under 19, you can use a query like this:
select name, count(*)
from course c
inner join registration r
on c.cid = r.courseid
where exists (
select 1
from course cc
inner join registration r
on cc.cid = r.courseid
inner join student s
on s.sid = r.studentid
where cc.cid = c.cid
group by cc.cid
having min(s.age) < 20
)
group by name;
Again with the updated fiddle here: http://sqlfiddle.com/#!9/c3b8f/17

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;