Average grade in sql server manager studio (SSMS) - sql

I have to calculate students with average grades bigger than 8 .
select students FROM table1
GROUP BY students
HAVING AVG(grade)>8;
When i run the code it doesn't get anything in return , no error , nothing , just column students with no values. I checked table1 to see if there're enough values to calculate and the values are there.
What is wrong with that , can you help me ?

You need to include the grade in your select statement. You can also provide a more accurate column name with as:
SELECT students, AVG(grade) as 'average'
FROM table1
GROUP BY Students
HAVING AVG(grade) > 8

The SELECT includes the values you want to see, while the HAVING filters the results down.
SELECT students, AVG(grade) "Average Grade" FROM table1
GROUP BY students
HAVING AVG(grade)>8;

Related

Calculate number of people over average

I am trying to calculate how many people have a grade higher than average.
What I have currently instead returns the number of students and when I delete "=" from ">=" it returns 0.
SELECT count(*)
FROM data.students
WHERE grade IN (SELECT grade
FROM data.students
GROUP BY grade HAVING grade >= AVG(grade));
If I put an integer instead of avg() function I get good results.
What am I doing wrong?
Computing the avg in a subquery is probably fastest:
SELECT count(*)
FROM data.students
WHERE grade > (SELECT avg(grade) FROM data.students);
> instead of >=, since you said "a grade higher than average".
What am I doing wrong?
In your subquery, GROUP BY grade aggregates to one row per distinct value of grade. avg(grade) is bound to be exactly the same as grade for every row (except grade IS NULL). Explains what you saw.
Your query was needlessly complex to begin with.
Try this :
SELECT count(*)
FROM (
SELECT grade >= AVG(grade) OVER () AS tst
FROM data.students
) AS a
WHERE a.tst= True

SQL Query Problem: How Many Students have one or more blood donations in 2016?

I am a high school student and am currently working on an exercise about SQL, which is this query that made me stuck for an hour and still couldn't solve it. The table schema is as follows:
Table
The question my teacher gave me was: How any Students have one or more blood donations in 2016?
Thanks for any generous help.
The SQL I tried to run is
SELECT COUNT(*)
FROM DONA
WHERE StudID IN (SELECT StudID
FROM DONA
WHERE YEAR(DonDate)= 2016
GROUP BY StudID
HAVING COUNT(*)>1)
GROUP BY StudID
And I still couldn't figure out how to COUNT the number of students.
You can join both the tables and use the keyword DISTINCT to find DISTINCT students-
SELECT COUNT(StudID)
FROM (SELECT DISTINCT StudID
FROM DONA D
WHERE YEAR(DonDate) = 2016) T;

How can I select distinct number of values from a column in SQL?

The database contains a REGISTRATION table and STUDENT table.
The columns of REGISTRATION table are :
CourseID, StudentID, CourseCode, Score, Year
The CourseCode column contains the codes of courses like CS-101, MS-202 (each student ID is registered to many courses). I need to find the names and ID's of students taking more than 3 courses.
I have tried:
Select distinct
CourseRegistrations.S_ID, Students.FirstName
from
Students, CourseRegistrations
where
Students.StudentID = CourseRegistrations.StudentID
group by
CourseRegistrations.S_ID, Students.FirstName
having
count(distinct CourseRegistrations.CourseCode) > 3
but this is showing all records of file.
You should try:
Select CourseRegistrations.StudentID, count(CourseRegistrations.CourseCode)
from Students, CourseRegistrations
where Students.StudentID=CourseRegistrations.StudentID
GROUP BY CourseRegistrations.StudentID
having count(CourseRegistrations.CourseCode)>3
Distinct is not need if you used GROUP BY
Select CusReg.S_ID
from Students stud join CourseRegistrations CusReg
ON stud.StudentID=CusReg.StudentID
GROUP BY CourseRegistrations.S_ID
having count(CusReg.CourseCode)>3
Dont Use OLD Style Joins
SOURCE

Find Name of person in a city with Maximum age

Here the following Query will give the result for Average Age and number of residents in a City. Group BY applied on City and Aggregated functions on residents as Count and Age as AVG.
select city, count(*) as residents, avg(age) as AverageAge
from people
group by city
Similarly I want the Name of the Resident in a City With Maximum Age
select city, name, max(age) as AverageAge
from people
group by city
But this query is not working as name is not there in the Group By clause and neither used as a Aggregated function
Can you please help me in this.
Not the cleanest way, but you can do:
SELECT P1.*
FROM People P1
JOIN
(
SELECT City, MAX(Age) AS Age
FROM People
GROUP BY City
) P2 ON P1.Age = P2.Age AND P1.City = P2.City
UPDATE: When taking execution plans into account, the most performant query I can figure out seems to be the one below; it also seems to give the result that makes the most sense, if there are multiple people of the same (highest) age, they will all be selected;
SELECT P1.*
FROM people p1
LEFT JOIN people p2
ON p1.city=p2.city AND p1.age < p2.age
WHERE p2.age IS NULL
Basically it'll use a simple left join to get all people that don't have anyone older in the same town.
Otherwise, the simplest to read "general" solution would be using a simple subquery to find the max age of anyone in the town and list people in the city of that age. If several people are of the same (highest) age, it will instead return a random one of them;
SELECT city, name, age
FROM people
WHERE age = (SELECT MAX(age) FROM people p WHERE p.city=people.city)
GROUP BY city;
I added the first query to Aaron's SQLfiddle here, as you can see the first query in this post is the only one of the three without a temporary or filesort.
Try
select city, name, max(age) as AverageAge
from people
group by city, name
sql group by query requires all the column names present in the select
You need a sub query, its a very useful technique sutable for a wide range of solutions. When they are needed they are the only effective solution. I always go to this page http://allenbrowne.com/subquery-01.html when I have forgotten the exact synatax I need (worth bookmarking as its not page 1 on google for sub query which it has been in the past) you want the "TOP n records per group" section. Hope this helps
Definition of "maximum age" : there is no individual (in the same city) with a higher age
SELECT pp.*
FROM people pp
WHERE NOT EXISTS (
SELECT *
FROM people px
WHERE px.city = pp.city
AND px.age > pp.age
)
;
Here is the easiest way to do it in my opinion:
SELECT P.city, P.name, P.age as AverageAge
FROM people P
WHERE P.age = (SELECT MAX(age)
FROM people.P2));

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.