SQL33 Find the student with the lowest GPA in each school

Knowledge point: Pitfalls of group by

Original question link
description
Title: Now the operation wants to find the students with the lowest GPAs in each school for research. Please take out the lowest GPAs in each school.

Insert image description here

Wrong answer:

SELECT device_id,university,MIN(gpa)
FROM user_profile
GROUP BY university 
ORDER BY university 

Writing this way will report an error:

“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘user_profile.device_id’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by”

The reason is that it violates the only_full_group_by principle. If you are doing your own project, of course you can change the configuration of MySQL to solve this problem. But now you are doing a question and you cannot modify the configuration of Niuke, so to put it bluntly, you are not allowed to use this question. Answer this way

[The pitfall of group by]
使用group by之后select 语句中不能出现常数、聚合函数、聚合键以外的字段, device_id is a field other than..., so an error occurred


The correct answer to this question:

select
  device_id,
  university,
  gpa
from
  user_profile
where
  (university, gpa) in (
    select
      university,
      min(gpa)
    from
      user_profile
    group by
      university
  )
order by
  university

Guess you like

Origin blog.csdn.net/QinLaoDeMaChu/article/details/128144460