How to get Total of counts? - sql

I'm trying to get Total number of count per column. Here is example of what I need:
Grade Count Name
9 1 Jon
10 3 Ash
I would like to get Total under my Count column what will give the sum of 1 and 3.
Here is my query:
select grade, count(*) as count, name
from students
group by grade, name
order by grade, name;
Thanks in advance.

Many databases support the ROLLUP clause. If yours does, you can just do something like this:
select grade, count(*) as count, name
from students
group by grade, name with rollup
order by grade, name;
Technically, you would want the columns where the grade and rollup are both NULL, but this will give you partial totals as well.

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;

Trying to get the max from a group of aggregates

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

Is this sql query correct? If incorrect how can I fix it?

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.

How to group in fixed count ranges?

Let's say I have one table with two columns (student_id,grade).
student_id is an integer and grade is a decimal number.
In case I have 1000 students and want to group them ordered by grade in groups of 10 students each.
Just to be clear, this should produce 100 groups. The first group cointains the 10 highest grades and the last group contains the 10 lowest grades.
How should I do that ?
Optimization is always welcome.
Thank you very much.
Joao
ntile will give a ranking by an amount of buckets.
select student_id, ntile(100) over (order by grade desc) from student

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;