I have a Table like that
and i want to see average score(if student have 3 scores for each course, student can join many course) for each student for each course.
But I couldn't create the sql query.
Are you just looking for aggregation?
select student_id, course_id, avg(score * 1.0) as avg_score
from exam_result
group by student_id, course_id
having count(*) >= 3;
Related
I have 2 tables enroll(sid, grade, dname, cno, sectno) and
student(sid, sname, sex, age, year, gpa)
I have to write query asking for the ids of students that took only sections that had more than 10 students in them.
Why this query is not giving me correct result?
select student.sid
from student
where student.sid IN (select student.sid
from enroll
group by sectno, cno, dname
having count (distinct enroll.sid) > 10)
What about this query, is it correct?
select distinct sid
from enroll e1
where 10 < (select count (*)
from enroll e2
where e2.sectno = e1.sectno
and e2.cno = e1.cno
and e2.dname = e1.dname)
order by sid
Have a sub-query that returns sectno's for sections with less than 10
students. Do NOT IN that result.
select distinct sid
from enroll
where sectno not in (select sectno
from enroll
group by sectno
having count(sid) < 10)
Try using something like the below:
select sid
from enroll e1
where (select count (sectionId) from enroll e2) > 10
order by sid ASC
Note: Don't use distinct in this scenario.
Here my schema: Exams(int students, int scores)
I am writing a SQL query to find the greatest spread between a student's score.
I have been able to generate a query that has the student id and their spread using this query:
select student, max(score) - min(score) from exams group by student;
Now this is where I am stumped. How do I get the maximum value of the spreads? More specifically, I don't really understand what to put in my select statement that would be outside my initial query.
You can also use a subquery:
select max(spread)
from (
select student, max(score) - min(score) as spread from exams group by student
) x;
Try this:
select student, max(score) - min(score) as SPREAD
from exams
group by student
order by (max(score) - min(score)) DESC
This will at least show the results from highest to lowest
SELECT campus,semester, AVG(CountOfStudents)
FROM
(
SELECT semester,year,campus, count(*) as CountOfStudents
FROM regestration
GROUP BY semester, year, campus,student_id
) t
GROUP BY campus,semester
I need to do is to find the average number of people per semester of each campus
My table structure is:
Table name - registration
student_id
campus
year
batch
semester
campus, year, semester and batch these can help identify unique records, where as student_id may repeat itself the query above gives wrong answer.
Follow these steps:
remove student_ID from the GROUP BY clause and
add DISTINCT inside COUNT()
query,
SELECT campus, semester, AVG(CountOfStudents)
FROM
(
SELECT semester, year, campus, count(DISTINCT student_id) as CountOfStudents
FROM registration
GROUP BY semester, year, campus
) t
GROUP BY campus, semester
Schema:
Student(studentid,name,age)
Course(coursename,dept)
enroll(studentid,course,grade)
I need to find , for students in each age group find their average grade for courses they have taken for Political Science and History, and return the names of student with max average grade for each age group
My attempt so far is :
select max(grade), age, name
from (
select name, age, grade
from student s, (
select avg(grade) as grade, e.studentid
from enroll e
where dname in ('Political Sciences', 'History')
group by studentid
) as temp
where s.studentid = temp.studentid
) temp1
group by temp1.age;
I want to know if logically it is correct, and not syntactically.
Here's a few tips regarding your query:
Be careful with your table aliases. Make sure that you carry them over to your SELECT
You can only include columns in your SELECT that are being used in your aggregate (GROUP BY). Therefore, you can't GROUP BY temp1.age and SELECT age, name
The logic behind your SQL looks solid to me, so long as "Age" correlates to "Age Group", and does not refer to the individual student's age.
i have a table that "users"
i want to count their salary who have ID 1 and 2
how i can count this in mysql
Have a look at Aggregate Functions in MySQL and GROUP BY (Aggregate) Functions
to get total salary of both
SELECT SUM(salary) FROM users WHERE id IN(1, 2);
and to get individual sum of salary
SELECT SUM(salary) FROM users WHERE id IN(1, 2) group by id;
SELECT SUM(salary) FROM users WHERE id IN(1, 2);
I shall refer you to counting rows from the mysql documentation
probably goes like:
SELECT COUNT(*) FROM users WHERE id IN (1,2);
although if you're actually trying to sum the salaries then you should refer to group by functions
SELECT id, SUM(salary) FROM users WHERE id IN (1,2) GROUP BY id;