Mysqlgroupby/having/distinct query example _ MySQL

Source: Internet
Author: User
The table content is as follows: query IDselectstuid, max (score) fromstu_select_class, and query the selectstuid and sum (score) fromstu_select_classgroupbystuidlimi tables with a total score of 2-3 in each subject:

// Query the person ID with the highest score

Select stuid, max (score) from stu_select_class;

// Query the total number of students in each subject.
Select stuid, sum (score) from stu_select_class group by stuid limit 1, 2;

// Query the highest score for each subject
Select classname, stuid, max (score) from stu_select_class group by classname;

// Students whose total query score is less than 140
Select stuid, sum (score) from stu_select_class group by stuid having sum (score) <140;

// The average score of the query is between 60 and 80.
Select stuid, avg (score) from stu_select_class group by stuid having avg (score) between 60 and 80;

// Query .... Students
Select stuid, sum (score), avg (score) from stu_select_class group by stuid having sum (score) <140 and avg (score) <80;
// Query the total score .. Number of students.
Select distinct count (stuid) from stu_select_class group by stuid having sum (score) <140 and avg (score) <80;
// Query... Number of students
Select count (distinct stuid) from stu_select_class where classname = 'hangzhou' and score <100;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.