Trying to get the max from a group of aggregates - sql

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

Related

output a number — the number of students whose name (first_name) matches the name of at least one other student

Well, that is, there are two Johns and one Quill, you need to output the number of those people who have the same name. In one column there should be the total number of students with the same names
SELECT COUNT(id) as count
FROM student
GROUP BY LOWER(first_name) HAVING COUNT(LOWER(first_name)) > 1;
it will output for each name the count, how to make the total?
In order to get the total, select from your query result and add the counts up.
SELECT SUM(cnt)
FROM
(
SELECT COUNT(*) AS cnt
FROM student
GROUP BY LOWER(first_name)
HAVING COUNT(*) > 1
) counts;
Please, try the following:
SELECT SUM(COUNT(id)) as Total
FROM student
GROUP BY LOWER(first_name) HAVING COUNT(LOWER(first_name)) > 1;

Using Count and Average in one Query

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;

Postgresql getting other information from max tuple

Say you have a table with studentID, class, grade. I want the maximum grade for each class. This is easy, just group by class and get max(grade). But the problem I'm having is how to also get the studentID.
Instead of using an aggregate function, you could use window functions:
SELECT class, grade, studentId
FROM (SELECT class, grade, studentId,
RANK() OVER (PARTITION BY class ORDER BY grade DESC) rk
FROM students)
WHERE rk = 1
I think distinct on is a good way to go:
select distinct on (s.class) s.*
from students
order by s.class, s.grade desc;
However you probably want all students for each class with the maximum grade. If so, Mureinik's solution is better.

Get details semester wise highest marks without cursor

I have two tables.
Student (Roll_id,Student_name)
Student_mark (roll_id, semester, marks, subject)
I want student details who has got highest marks IN semester AND subject wise WITH their roll_id,mark,semester,subject
using that two table.
can we get results without using cursor?.
You can use the RANK() function to put the students in order by mark (partitioned by semester and subject):
WITH RankedResults AS
( SELECT s.Roll_ID,
s.Student_Name,
sm.Semester,
sm.Subject,
sm.Marks,
StudentRank = RANK() OVER(PARTITION BY sm.Semester, sm.Subject ORDER BY sm.Marks DESC)
FROM Student s
INNER JOIN Student_Mark sm
ON s.Roll_ID = sm.Roll_ID
)
SELECT Roll_ID, Student_name, Semester, Subject, Marks
FROM RankedResults
WHERE StudentRank = 1;
Example on SQL Fiddle

how to make counting in mysql

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;