Selecting total number of students in each class - mysql

I have two tables:
Class: cid, name, location
Student: sid, name, gender, classID
I am attempting to use a select statement to display the class name along with the total number of students in the class. I'm also trying to exclude any class with less than 10 students from the table.

SELECT
c.name as className,
count(s.sid) as totalStudents
FROM Class c
JOIN Student s ON s.classID = c.cid
GROUP BY c.id
HAVING count(s.sid) >= 10

select a.name, count(*)
from Class as a
inner join Student as b on b.ClassID = a.cid
group by a.name
having count(*) >= 10;

Related

query that select distinct elements

I want to print the name of all companies that have done at least 3 sponsorizations on different cities
ps: kid is primary key of company
This is what I tied so far but this return all companies that has at least 3 sponsorizations but not filter about different cities.
company(kid, name)
sponsorization(kid, city, sum_of_sponsorization)
SELECT c.name, count(c.name)
FROM COMPANY c
INNER JOIN SPONSORIZATION s
ON c.kid = s.kid
GROUP BY
c.name
having count(c.name) > 3
You can try below- with count(distinct s.city)
SELECT c.name
FROM COMPANY c
INNER JOIN SPONSORIZATION s ON c.kid = s.kid
GROUP BY
c.name
having count(distinct s.city) > 3
count distinct city
SELECT c.name
FROM COMPANY c
INNER JOIN SPONSORIZATION s
ON c.kid = s.kid
GROUP BY
c.name
having count(distinct s.city) > 3

Finding duplicate values in mysql when joining two tables

I have the following 3 tables for students and I need to find the names of students who currently are enrolled in classes that meet at the same time
student(**snum**, sname, major, level, age)
class(**cname**, meets_at, room)
enroll(**snum**, **cname**)
The keys for each table is in bold.
I have tried the folling code and I'm not sure whether I'm close to the correct answer here.
select s.sname
from student s
join
( select c.cname
, c.meets_at
, (count(*)
from class c
having count( * ) > 1
) e
on c.cname = e.cname
and s.snum = e.snum;
I would think of this as joining on students with meetsat. So, this gets students with their class times:
select s.sname, c.meets_at
from students s join
enrolls e
on s.snum = e.snum join
classes c
on c.cname = e.cname;
Then, to get the duplicates, use aggregation and filter using having:
select s.snum, s.sname, c.meets_at, count(*) as cnt
from students s join
enrolls e
on s.snum = e.snum join
classes c
on c.cname = e.cname
group by s.snum, sname, c.meets_at
having count(*) >= 2;
Note that this includes the ids as well as the name, because two students could have the same name.
Finally, a student could have multiple pairs of classes that conflict, but you only want to see the student once. Although the above is probably sufficient for your purposes, a more accurate solution for the stated problem would be:
select distinct s.sname
from students s join
enrolls e
on s.snum = e.snum join
classes c
on c.cname = e.cname
group by s.snum, sname, c.meets_at
having count(*) >= 2;
select s.snum, s.sname, c.meets_at ,count(*) as cnt into #tempStudents
from students s
join enrolls e on s.snum = e.snum
join classes c on c.cname = e.cname;
Put the whole data in temporary table
select sname from #tempStudents
group by snum,sname ,meets_at
having cnt >=2
Now apply filtration on temporary table for getting desire data
and if u need distinct name of student put Distinct keyword before sname.

how to display the name of the departments that has the least student count

how to write a query to display the name of the departments that have the least student count. Sort the result based on department name in ascending order
select d.department_name from
(select dd.department_name, count(di.department_id) as id from student di
join department dd on di.department_id=dd.department_id group by dd.department_name) d,
(select min(count(*)) as new from student group by department_id) d2
where d.id=d2.new;
select d.department_name from Department d, Student s where
d.department_id = s.department_id
group by d.department_name
having count(s.student_id)<=all
(select count(s.student_id) from Department d, Student s where
d.department_id = s.department_id
group by d.department_name)
order by department_name;
Try this.
select d.department_id, d.department_name
from Department d
join Student s on d.department_id = s.department_id
group by d.department_id
having count(s.student_id) = (select min(count(s2.student_id))
from student s2
join department d2
on s2.department_id = d2.department_id
group by d2.department_id)
order by d.department_name
You must join the 2 tables to have the needed information.
You'll also have to group them by the selected information such that you can count the students.
And lastly, you place the condition. Needing a subquery to retrieve the minimum number of students.
select department_name
from Department
join Student
on Department.department_id=Student.department_id
having count(*) in
( select min(count(*)) from Student group by department_id)
group by Department.department_id,department_name
order by department_name asc;

comparing averages in mysql

I have two tables; One that contains students names and sids and the other is a "take" table containing the sids of students and their grades.
I want to show the names of students who have average greater than that of a student name "Peter-Parker".
I have tried the query below, but it doesn't work.
SELECT s.sid, s.fname, s.lname
FROM student s, take t
WHERE s.sid = t.sid AND AVG(t.grade) > ALL(
SELECT AVG(grade)
FROM take, student
WHERE student.fname = 'Ali' and student.lname='Demir');
WITH AliAv(avg) AS
(SELECT AVG(grade) from take t, student s
where t.sid = s.sid ands.fname = 'Ali' and s.lname = 'Demir')
select student.sid, student.fname, student.lname
from student, take
where student.sid = take.sid Group by student.sid
having avg(take.grade) > AliAv.av;
SELECT s.sid, s.fname, s.lname, AVG(t.grade) AS average
FROM student AS s
JOIN take AS t ON t.sid = s.sid
GROUP BY s.sid
HAVING average > (
SELECT AVG(t2.grade)
FROM student AS s2
JOIN take AS t2 ON t2.sid = s2.sid
WHERE s2.fname = 'Peter' and s2.lname = 'Parker'
)
This works in SQL Server, I don't know if the syntax is valid in MySQL:
SELECT *
FROM Student s
WHERE
(SELECT AVG(Grade) FROM Take WHERE SID=s.ID) > (SELECT AVG(Grade) FROM Take WHERE SID = (SELECT SID FROM Student WHERE FName='Peter' AND LName='Parker'))

MySQL count maximum number of rows

I am trying to select the classes with maximum enrollment in each department from a table with the following structure:
Courses: cid, dept, name
Enrollment: cid, sid
The idea would be to count all the enrollments in each class, and find the maximum in each department. Unfortunately, I'm having trouble with the maximum-count combination.
My query was going to look something like:
SELECT c.name, MAX(COUNT(*) FROM Enrollment E WHERE E.cid = C.cid)
FROM Courses C
GROUP BY C.dept
But I can't figure out how to get the MAX-COUNT syntax to work properly. I've tried a lot of different examples from Google and StackOverflow and none of them worked for me. MySQL says the syntax is wrong.
I like nested queries for this kind of problem. First select the enrollment counts grouped per class. Then find the max enrollment count grouped per department:
SELECT MAX(cnt) cnt, dept FROM
(
SELECT COUNT(*) cnt, dept, C.cid
FROM Courses C
INNER JOIN Enrollment E on C.cid = E.cid
GROUP BY cid, dept
) a
GROUP BY dept
There is no declaration of E, so you can't use E.cid
So, either you do this
SELECT c.name, COUNT(c.*) as count
FROM Courses C
GROUP BY C.dept
Or,
SELECT c.name, MAX(SELECT COUNT(*) FROM Enrollment E WHERE E.cid = C.cid)
FROM Courses C
GROUP BY C.dept
SELECT MAX(cnt) FROM (
SELECT c.name, COUNT(E.*) AS cnt FROM Enrollment E
INNER JOIN Courses C ON E.cid = C.cid
GROUP BY C.dept
) final