Home > Software engineering >  SQL to return all students with minimum GPA
SQL to return all students with minimum GPA

Time:11-29

I'm trying to do a query which, as the title says, returns the name of all the students that share the minimum GPA.

Since the minimum GPA from all the students I have in the table is 0, right now I have:

Select s.name from Student s
where s.gpa = 0

However, I don't want to put the value '0' since it could not always be the case, instead I'm trying to find the minimum gpa with MIN(gpa)

I've been trying to use that aggregate function in combination with group by But I cannot make it work. So far I have this:

Select s.name from Student s
having s.gpa = min(s.gpa)
group by s.gpa

How should I change it to make it work? Thanks in advance

CodePudding user response:

You need to first find what is the minimum GPA, then get students that have it.

To get the minimum GPA in the intire table:

SELECT MIN(gpa) FROM Student;

This query can be used as a nested query inside your original query:

SELECT name FROM Student
WHERE gpa = (SELECT MIN(gpa) FROM Student);

I am not sure if there is a simpler way to do it. I hope this helps!

  • Related