MySQL queries to get the top three student information for each class score

Source: Internet
Author: User

Today, some colleagues talked about how to find the MySQL query to get the top three students of each class, in the online search, and then write code, and soon OK.

CREATE TABLE t_testscore (pk_id int PRIMARY KEY, C_name VARCHAR (), C_score int, c_class int) DEFAULT Charse T=utf8;

INSERT into T_testscore VALUES (1, ' 36 ', 66, 1), (2, ' Zhang 35 ', 65, 1), (3, ' 34 ', 64, 1), (4, ' 33 ', 63, 1), (5, ' 32 ', 62, 1);

INSERT into T_testscore VALUES (11, ' Li 46 ', 76, 2), (12, ' Li 45 ', 75, 2), (13, ' Li 44 ', 74, 2), (14, ' Li 43 ', 73, 2), (15, ' Li 42 ', 72 , 2);

SELECT * from T_testscore t

where EXISTS (SELECT COUNT (*) from T_testscore ts WHERE ts.c_score>=t.c_score GROUP by Ts.c_class have COUNT (*) < =3)

ORDER by C_class,c_score DESC;

MySQL queries to get the top three student information for each class score

Related Article

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.